zoukankan      html  css  js  c++  java
  • LA 4287

    Consider the following exercise, found in a generic linear algebra textbook.

    Let A be an n × n matrix. Prove that the following statements are equivalent:
    1. A is invertible.
    2. Ax = b has exactly one solution for every n × 1 matrix b.
    3. Ax = b is consistent for every n × 1 matrix b.
    4. Ax = 0 has only the trivial solution x = 0.

    The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent.

    Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications!

    I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?

    Input

    On the first line one positive number: the number of testcases, at most 100. After that per testcase:

    • One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved.
    • m lines with two integers s1 and s2 (1 ≤ s1s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.

    Output

    Per testcase:

    • One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.

    Sample Input

    2
    4 0
    3 2
    1 2
    1 3
    

    Sample Output

    4
    2
    
    The 2008 ACM Northwestern European Programming Contest
     
    强连通缩点,求最少添加多少条边使图强连通,设in 入度为0的数目 ou 出度, 答案便是max(in,ou) 特殊情况是 当只有一个点时为0
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <stack>
     6 
     7 using namespace std;
     8 
     9 const int MAX_N = 20005;
    10 const int edge = 5e4 + 5;
    11 int N,M;
    12 int low[MAX_N],pre[MAX_N],cmp[MAX_N];
    13 int first[MAX_N],Next[edge],v[edge];
    14 int ind[MAX_N],oud[MAX_N];
    15 int dfs_clock,scc_cnt;
    16 stack <int > S;
    17 
    18 void add_edge(int id,int u) {
    19         int e = first[u];
    20         Next[id] = e;
    21         first[u] = id;
    22 }
    23 
    24 void dfs(int u) {
    25         low[u] = pre[u] = ++dfs_clock;
    26         S.push(u);
    27         for(int e = first[u]; e != -1; e = Next[e]) {
    28                 if(! pre[ v[e] ]) {
    29                         dfs(v[e]);
    30                         low[u] = min(low[u],low[ v[e] ]);
    31                 } else if(!cmp[ v[e] ]) {
    32                         low[u] = min(low[u],pre[ v[e] ]);
    33                 }
    34         }
    35 
    36         if(low[u] == pre[u]) {
    37                 ++scc_cnt;
    38                 for(;;) {
    39                         int x = S.top(); S.pop();
    40                         cmp[x] = scc_cnt;
    41                         if(x == u) break;
    42                 }
    43         }
    44 }
    45 
    46 void scc() {
    47         int dfs_clock = scc_cnt = 0;
    48         memset(cmp,0,sizeof(cmp));
    49         memset(pre,0,sizeof(pre));
    50 
    51         for(int i = 1; i <= N; ++i) {
    52                 if(!pre[i]) dfs(i);
    53         }
    54 
    55         for(int i = 1; i <= N; ++i) {
    56                 for(int e = first[i]; e != -1; e = Next[e]) {
    57                         if(cmp[i] == cmp[ v[e] ]) continue;
    58                         ind[ cmp[ v[e] ] ]++;
    59                         oud[ cmp[i] ]++;
    60                 }
    61         }
    62 
    63         int in = 0,ou = 0;
    64         for(int i = 1; i <= scc_cnt; ++i) {
    65                 in += !ind[i];
    66                 ou += !oud[i];
    67         }
    68         printf("%d
    ",scc_cnt == 1 ? 0 : max(in,ou));
    69 }
    70 int main()
    71 {
    72     //freopen("sw.in","r",stdin);
    73     int t;
    74     scanf("%d",&t);
    75     while(t--) {
    76             memset(ind,0,sizeof(ind));
    77             memset(oud,0,sizeof(oud));
    78 
    79             scanf("%d%d",&N,&M);
    80             for(int i = 1; i <= N; ++i) first[i] = -1;
    81             for(int i = 1; i <= M; ++i) {
    82                     int u;
    83                     scanf("%d%d",&u,&v[i]);
    84                     add_edge(i,u);
    85             }
    86 
    87             scc();
    88 
    89     }
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    Cookie与Session
    发布网站
    WCF服务寄宿Windows
    JQuery:各种操作表单元素方法小结
    setTimeout()与 setInterval()
    CSS样式
    循环获取<ul>下拉列表的的值。进行对比,正确的家样式
    js定时器 实现提交成功提示
    flask 实现登录 登出 检查登录状态 的两种方法的总结
    flask 状态保持session和上下文session的区别
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3700871.html
Copyright © 2011-2022 走看看