标签:
input | output |
---|---|
6 3 4 1 2 2 3 4 5 1 3 3 4 4 6 5 6 |
2 |
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 inline void SetIO(string Name) 38 { 39 string Input = Name+".in", 40 Output = Name+".out"; 41 freopen(Input.c_str(), "r", stdin), 42 freopen(Output.c_str(), "w", stdout); 43 } 44 45 46 inline int Getint() 47 { 48 int Ret = 0; 49 char Ch = ‘ ‘; 50 bool Flag = 0; 51 while(!(Ch >= ‘0‘ && Ch <= ‘9‘)) 52 { 53 if(Ch == ‘-‘) Flag ^= 1; 54 Ch = getchar(); 55 } 56 while(Ch >= ‘0‘ && Ch <= ‘9‘) 57 { 58 Ret = Ret * 10 + Ch - ‘0‘; 59 Ch = getchar(); 60 } 61 return Flag ? -Ret : Ret; 62 } 63 64 const int N = 10010, M = 24010; 65 int n, m, k; 66 int Fa[N]; 67 inline void Input() 68 { 69 scanf("%d%d%d", &n, &m, &k); 70 } 71 72 inline int Find(int x) 73 { 74 static int Arr[N], Length; 75 Length = 0; 76 while(x != Fa[x]) 77 { 78 Arr[++Length] = x; 79 x = Fa[x]; 80 } 81 For(i, 1, Length) Fa[Arr[i]] = x; 82 return x; 83 } 84 85 inline void Solve() 86 { 87 For(i, 1, n) Fa[i] = i; 88 For(i, 1, m) 89 { 90 int u, v; 91 scanf("%d%d", &u, &v); 92 u = Find(u), v = Find(v); 93 if(u != v) Fa[u] = v; 94 } 95 96 int Ans = 0; 97 For(i, 1, n) 98 if(Find(i) == i) Ans++; 99 printf("%d\n", Ans - 1); 100 } 101 102 int main() 103 { 104 #ifndef ONLINE_JUDGE 105 SetIO("H"); 106 #endif 107 Input(); 108 Solve(); 109 return 0; 110 }
ural 1272. Non-Yekaterinburg Subway
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/4999067.html