Title Link: Http://codeforces.com/contest/999/problem/E
Topic:
Test instructions: give you n a city, M one-way side, ask you how many sides you need to add to make it from the capital S can reach any city.
Idea: Tarjan, the result is the number of points in the newly created graph with a scale of 0.
The code is implemented as follows:
1#include <Set>2#include <map>3#include <queue>4#include <stack>5#include <cmath>6#include <bitset>7#include <cstdio>8#include <string>9#include <vector>Ten#include <cstdlib> One#include <cstring> A#include <iostream> -#include <algorithm> - using namespacestd; the -typedefLong Longll; -typedef PAIR<LL, Ll>PLL; -typedef PAIR<LL,int>Pli; +typedef pair<int, ll>pil;; -typedef pair<int,int>PII; +typedef unsignedLong Longull; A at #defineLson i<<1 - #defineRson i<<1|1 - #defineBug printf ("*********\n"); - #defineFIN freopen ("D://code//in.txt "," R ", stdin); - #defineDebug (x) cout<< "[" <<x<<] "<<endl; - #defineIO Ios::sync_with_stdio (False), Cin.tie (0); in - Const DoubleEPS = 1e-8; to Const intMoD =10007; + Const intMAXN = 5e3 +7; - Const DoublePI = ACOs (-1); the Const intINF =0x3f3f3f3f; * Constll INF =0x3f3f3f3f3f3f3f; $ Panax Notoginseng intN, M, tot, S, cnt, top, U, V, num; - intHEAD[MAXN], VIS[MAXN],inch[MAXN]; the inttot1, HEAD1[MAXN], IN1[MAXN]; + intDFN[MAXN], LOW[MAXN], C[MAXN], STC[MAXN]; A the structEdge { + intV, next; - }ED[MAXN], ED1[MAXN]; $ $ voidAddedge (intUintv) { -ED[TOT].V =v; -Ed[tot].next =Head[u]; theHead[u] = tot++; - }Wuyi the voidAddedge1 (intUintv) { -ED1[TOT1].V =v; WuEd1[tot1].next =Head1[u]; -Head1[u] = tot1++; About } $ - voidTarjan (intx) { -DFN[X] = low[x] = + +num; -VIS[X] =1, stc[++top] =x; A for(inti = head[x]; ~i; i =Ed[i].next) { + inty =ed[i].v; the if(!Dfn[y]) { - Tarjan (y); $LOW[X] =min (low[x], low[y]); the}Else if(Vis[y]) { theLOW[X] =min (low[x], low[y]); the } the } - if(Dfn[x] = =Low[x]) { in intY cnt++; the Do { they = stc[top--]; Vis[y] =0; AboutC[y] =CNT; the} while(X! =y); the } the } + - intMain () { the //FIN;Bayiscanf"%d%d%d", &n, &m, &s); theMemset (Head,-1,sizeof(head)); thememset (Head1,-1,sizeof(Head1)); - for(inti =1; I <= m; i++) { -scanf"%d%d", &u, &v); the Addedge (U, v); the inch[v]++; the } the for(inti =1; I <= N; i++) { - if(inch[I] = =0&&!Dfn[i]) { the Tarjan (i); the } the }94 for(inti =1; I <= N; i++) { the if(!Dfn[i]) { the Tarjan (i); the }98 } About intsum =0; - for(inti =1; I <= N; i++) {101 for(intj = Head[i]; ~j; j =Ed[j].next) {102 inty =ed[j].v;103 if(C[i] = = C[y])Continue;104 Addedge1 (C[i], c[y]); thein1[c[y]]++;106 } 107 }108s =C[s];109 for(inti =1; I <= CNT; i++) { the if(I! = s && in1[i] = =0) {111sum++; the }113 } theprintf"%d\n", sum); the return 0; the}
Reachability from the capital (codeforces Round #490 (Div. 3) +tarjan to indent points)