zoukankan      html  css  js  c++  java
  • hdu 2767 Proving Equivalences

    Proving Equivalences

    Time Limit: 2000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2767
    64-bit integer IO format: %I64d      Java class name: Main
     
    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 ≤ s1, s2 ≤ 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

    Source

     
    解题:强连通缩点。。。典型题
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 20010;
     4 vector<int>g[maxn];
     5 int dfn[maxn],low[maxn],id[maxn],od[maxn];
     6 int belong[maxn],scc,idx;
     7 bool instack[maxn];
     8 stack<int>stk;
     9 void init() {
    10     for(int i = 0; i < maxn; ++i) {
    11         dfn[i] = belong[i] = 0;
    12         id[i] = od[i] = 0;
    13         instack[i] = false;
    14         g[i].clear();
    15     }
    16     scc = idx = 0;
    17     while(!stk.empty()) stk.pop();
    18 }
    19 void tarjan(int u) {
    20     dfn[u] = low[u] = ++idx;
    21     instack[u] = true;
    22     stk.push(u);
    23     for(int i = g[u].size()-1; i >= 0; --i) {
    24         if(!dfn[g[u][i]]) {
    25             tarjan(g[u][i]);
    26             low[u] = min(low[u],low[g[u][i]]);
    27         } else if(instack[g[u][i]]) low[u] = min(low[u],dfn[g[u][i]]);
    28     }
    29     if(dfn[u] == low[u]) {
    30         scc++;
    31         int v;
    32         do {
    33             instack[v = stk.top()] = false;
    34             stk.pop();
    35             belong[v] = scc;
    36         } while(v != u);
    37     }
    38 }
    39 int main() {
    40     int kase,n,m,u,v;
    41     scanf("%d",&kase);
    42     while(kase--) {
    43         scanf("%d%d",&n,&m);
    44         init();
    45         for(int i = 0; i < m; ++i) {
    46             scanf("%d%d",&u,&v);
    47             g[u].push_back(v);
    48         }
    49         for(int i = 1; i <= n; ++i)
    50             if(!dfn[i]) tarjan(i);
    51         if(scc <= 1) {
    52             puts("0");
    53             continue;
    54         }
    55         for(int i = 1; i <= n; ++i)
    56             for(int j = g[i].size()-1; j >= 0; --j)
    57                 if(belong[i]!=belong[g[i][j]]) {
    58                     id[belong[g[i][j]]]++;
    59                     od[belong[i]]++;
    60                 }
    61         int a = 0,b = 0;
    62         for(int i = 1; i <= scc; ++i) {
    63             if(!id[i]) a++;
    64             if(!od[i]) b++;
    65         }
    66         printf("%d
    ",max(a,b));
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    TabControl 切换 内嵌web页面直接响应滚动事件
    进程、应用程序域和对象上下文
    CSharp中的多线程——线程同步基础
    CSharp中的多线程——入门
    注重实效的程序员之快速参考指南
    学习语言技术快速入门——五步骤
    利用jQuery选择将被操作的元素
    CSharp中的多线程——使用多线程
    android开发文件介绍
    三角函数公式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4693022.html
Copyright © 2011-2022 走看看