Codeforces 506b/505d Mr Kitayuta ' s technology

Source: Internet
Author: User
Tags cmath

Portal:Http://codeforces.com/problemset/problem/506/B

Http://codeforces.com/problemset/problem/505/D

Good question

To n cities, M-Bar has a forward edge, to find the least forward edge so that its composition of the figure and the original image and other potential

For each connected component:

If there is no ring, then only need to n-1 edge to complete the Unicom

If there is a ring, then only the N-side to complete the Unicom

So as long as the connected component is sentenced, then there are several connected components have a ring can

Solution One: undirected graph traverse to find strong connected components and then connect the strong connected components represented by the Unicom Component DFS Award ring, as follows

memory:10440 KB time:498 MS
1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <vector>8 using namespacestd;9 #definefor (i,j,k) for (int i=j;i<=k;i++)Ten #defineFORD (i,j,k) for (int i=j;i>=k;i--) One #defineLL Long Long A #defineSZ (x) int (x.size ()) - #defineMAXN 100010 - intn,m,x,y; the BOOLcircle=true; - intVIS[MAXN],VIS2[MAXN]; -vector<int>NEG[MAXN],ADJ[MAXN]; -vector<int>dot; + voidNDFs (intstart) - { + dot.push_back (start); Avis[start]=1; atfor (I,0, SZ (Neg[start])-1) -     { -         if(!Vis[neg[start][i]] NDFs (Neg[start][i]); -          -     } -     return; in } - voidADFsintstart) to { +vis2[start]=1; -for (I,0, SZ (Adj[start])-1) the     { *         if(!Vis2[adj[start][i]]) ADFS (Adj[start][i]);  $         Else if(vis2[adj[start][i]]==1) circle=false; Panax Notoginseng     } -vis2[start]=2; the     return; + } A intMain () the { +Cin>>n>>m; -for (I,1, M) $ { $Cin>>x>>y; - neg[x].push_back (y); - neg[y].push_back (x); the adj[x].push_back (y); - }Wuyi intans=N; thefor (I,1, N) - { Wucircle=1; -     if(!Vis[i]) About     { $ dot.clear (); - NDFs (i);  -for (I,0, SZ (dot)-1) -     if(!Vis2[dot[i]]) ADFS (Dot[i]); Aans-=Circle; +     } the } -cout<<ans<<Endl; $ return 0; the}
I'm wondering if the name of the function is to take AFS or ADFS.

Solution Two: In the undirected graph maintenance and check set to find strong connectivity components and the strong connected components of the Unicom components represented by topological sequencing of the ring, as follows

memory:7820 KB time:514 MS
1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <vector>8 using namespacestd;9 #definefor (i,j,k) for (int i=j;i<=k;i++)Ten #defineFORD (i,j,k) for (int i=j;i>=k;i--) One #defineLL Long Long A #defineSZ (x) int (x.size ()) - #defineMAXN 100010 -vector<int>G[MAXN]; the intn,m,x,y,t; - intFATHER[MAXN],VAL[MAXN],CIRCLE[MAXN],VIS[MAXN],CLOCK[MAXN]; - intSetfind (intx) - { +     intHasFather[x]; -     if(fa==x)returnx; +     Else returnfather[x]=Setfind (FA); A } at voidSetunion (intXinty) - { -     intx=setfind (x); -     inty=Setfind (y); -     if(x==y)return; -     if(Val[x]>val[y]) father[y]=X; in     Elsefather[x]=Y; -     if(Val[x]==val[y]) val[x]++; to     return; + } - voidDfsintstart) the { *vis[start]=1; $for (I,0, SZ (G[start])-1)Panax Notoginseng     if(!Vis[g[start][i]]) DFS (g[start][i]); -clock[start]=++T; the     return; + } A intMain () the { +Cin>>n>>m; -for (I,1, N) ${father[i]=i;val[i]=1;} $for (I,1, M) - { -Cin>>x>>y; the g[x].push_back (y); - setunion (x, y);Wuyi } thefor (I,1, N) - if(!Vis[i]) DFS (i); Wufor (I,1, N) -For (J,0, SZ (G[i])-1) About if(Clock[i]<clock[g[i][j]]) circle[setfind (i)]=1; $ intans=N; -for (I,1, N) - if(I==setfind (i))if(!circle[setfind (i)]) ans--; -cout<<ans<<Endl; A return 0; +}
Mr Kitayuta ' s Black technology

In fact, the connectivity component can also be dyed

  To be able to use and check the set.

Anyway, it's all going to be a mess.

Codeforces 506b/505d Mr Kitayuta ' s technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.