Codeforces Round #286 (Div. 1 B)

Source: Internet
Author: User

Http://codeforces.com/contest/506/problem/BProblem a graph also has n points, to M-relations < A, B, to indicate that a must arrive at B., the relationship satisfies transitivity, i.e. a->b,b->c , then A->c. At the same time, a to b,b not necessarily to a. Ask, at least how many edges to add, so that each relationship is set up. Solution for an n-point graph, because the edge is transitive, it may be possible to connect the n points into a ring, then must be 22 to each other, but this is not necessarily the least. For a weak link graph with n points, if it does not have a ring (it is a dag), then only the n-1 edge is needed (think about whether n points can be turned into a "chain" after you sort the DAG topology); if it exists, then turn it into a ring and need n edges. The algorithm is as follows: According to the given m relationships, the a->b constructs an edge. DFS, judging whether each weak link graph has a ring, then the statistical answer. In which to use, DFS awarded to the graph ring, and check the integration of weak unicom map. My Code
//hello. I ' m Peter.#pragma COMMENT (linker, "/stack:102400000,102400000")#include <cstdio>#include <iostream>#include <sstream>#include <cstring>#include <string>#include <cmath>#include <cstdlib>#include <algorithm>#include <functional>#include <cctype>#include <ctime>#include <stack>#include <queue>#include <vector>#include <set>#include <map>using namespace STD;typedef Long Longll#define Peter cout<< "I am Peter" <<endl#define INPUT Freopen ("Data.txt", "R", stdin)#define Randin srand ((unsigned int) time (NULL))#define INT (0x3f3f3f3f) * *#define LL (0x3f3f3f3f3f3f3f3f)#define MAXN 100010#define N 100100#define Minline intRead () {intx=0, f=1;CharCh=getchar (); while(ch>' 9 '|| ch<' 0 '){if(ch=='-') f=-1; Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x=x*Ten+ch-' 0 '; Ch=getchar ();}returnX*f;}intN,m;intNex[n],nexnum[n];inline voidInit_nex () { for(intI=1; i<=n;i++) {nex[i]=i; nexnum[i]=1; }}inline intFindnex (intx) {if(nex[x]!=x) Nex[x]=findnex (nex[x]);returnNEX[X];}structedge{intFrom,to,next;} edge[maxn<<1];intHead[n],num_edge;inline voidInit_edge () { for(intI=1; i<=n;i++) {head[i]=-1; } num_edge=0;}inline voidAdd_edge (intFromintTo) {intT=++num_edge;    Edge[t].from=from;    Edge[t].to=to;    Edge[t].next=head[from]; head[from]=t;}intVis[n],loop[n];BOOLOkinline voidDfsintNow) {if(!ok)return; vis[now]=1; for(inti=head[now];i!=-1; i=edge[i].next) {if(!ok)return;intto=edge[i].to;if(!vis[to]) DFS (to);Else if(vis[to]==1) {ok=false;return; }Else if(vis[to]==-1)Continue; } vis[now]=-1;}intMain () {N=read (), M=read ();    Init_nex (); Init_edge (); for(intI=1; i<=m;i++) {intA=read (), B=read (); Add_edge (A, b);intNex1=findnex (a);intNex2=findnex (b);if(NEX1!=NEX2)            {nex[nex1]=nex2;        NEXNUM[NEX2]+=NEXNUM[NEX1]; }    } for(intI=1; i<=n;i++) {intNex=findnex (i);if(loop[nex]| | Vis[i])Continue; ok=true; DFS (i);if(!ok) loop[nex]=true; }intans=0; for(intI=1; i<=n;i++) {intNex=findnex (i);if(vis[nex]!=-2) {vis[nex]=-2;if(Loop[nex]) Ans+=nexnum[nex];Elseans+=nexnum[nex]-1; }    }printf("%d\n", ans);return 0;}

Codeforces Round #286 (Div. 1 B)

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.