Proving Equivalences (hdu 2767 強聯通縮點)

來源:互聯網
上載者:User

標籤:parent   dex   require   mos   namespace   esc   str   state   near   

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3743    Accepted Submission(s): 1374


Problem DescriptionConsider 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?

 
InputOn 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. 
OutputPer 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

24 03 21 21 3
 
Sample Output
42
 
Source

field=problem&key=NWERC+2008&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">NWERC 2008 
Recommendlcy   |   We have carefully selected several similar problems for you:  2768 2766 2769 

pid=2773" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2773 

pid=2772" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2772  

題意:n個點m條邊,問最少加入多少條邊使得整個圖聯通。

思路:先Tarjan求強聯通分量,縮點,再求縮點後的點的入度和出度,入讀為0的點的個數為a。出度為0的點的個數為b,ans=max(a。b)

代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;const int MAXN = 20050;//點數const int MAXM = 500050;//邊數struct Edge{    int to,next;}edge[MAXM];int head[MAXN],tot;int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong數組的值是1~sccint Index,top;int scc;//強聯通分量的個數bool Instack[MAXN];int num[MAXN];//各個強聯通分量包括的點的個數。數組編號為1~scc//num數組不一定須要,結合實際情況void addedge(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}void Tarjan(int u){    int v;    Low[u]=DFN[u]=++Index;    Stack[top++]=u;    Instack[u]=true;    for (int i=head[u];i+1;i=edge[i].next)    {        v=edge[i].to;        if (!DFN[v])        {            Tarjan(v);            if (Low[u]>Low[v]) Low[u]=Low[v];        }        else if (Instack[v]&&Low[u]>DFN[v])            Low[u]=DFN[v];    }    if (Low[u]==DFN[u])    {        scc++;        do{            v=Stack[--top];            Instack[v]=false;            Belong[v]=scc;            num[scc]++;        }while (v!=u);    }}void solve(int N){    memset(DFN,0,sizeof(DFN));    memset(Instack,false,sizeof(Instack));    memset(num,0,sizeof(num));    Index=scc=top=0;    for (int i=1;i<=N;i++)      //點的編號從1開始        if (!DFN[i])            Tarjan(i);}void init(){    tot=0;    memset(head,-1,sizeof(head));}int n,m;int in[MAXN],out[MAXN];int main(){#ifndef ONLINE_JUDGE    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);#endif    int i,j,u,v,t;    sf(t);    while (t--)    {        sff(n,m);        if(n==1){   //特判1(n==1,m==0)            printf("0\n");            continue;        }        if(m==0){   //特判2( n==?

,m==0) printf("%d\n",n); continue; } init(); for (i=0;i<m;i++) { sff(u,v); addedge(u,v); } solve(n); if(scc==1){ //假設強連通個數為1 printf("0\n"); continue; } mem(in,0); mem(out,0); for (int u=1;u<=n;u++) { for (i=head[u];i+1;i=edge[i].next) { int v=edge[i].to; if (Belong[u]!=Belong[v]) { out[Belong[u]]++; in[Belong[v]]++; } } } int ans,a=0,b=0; for (i=1;i<=scc;i++) { if (out[i]==0) a++; if (in[i]==0) b++; } ans=max(a,b); pf("%d\n",ans); } return 0;}




Proving Equivalences (hdu 2767 強聯通縮點)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.