标签:ref inpu mem 有趣 time alt opened bool scanf
题目链接:http://poj.org/problem?id=1182
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 78133 | Accepted: 23275 |
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
题解:
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 5e4+10; 19 20 int n, m; 21 int fa[MAXN], r[MAXN]; 22 23 int find(int x) 24 { 25 if(fa[x]==-1) return x; 26 int pre = find(fa[x]); 27 r[x] = (r[x]+r[fa[x]])%3; 28 return fa[x] = pre; 29 } 30 31 bool Union(int w, int u, int v) 32 { 33 int fu = find(u); 34 int fv = find(v); 35 if(fu==fv) 36 return ((3-w+r[u])%3!=r[v]); 37 38 fa[fu] = fv; 39 r[fu] = (3-r[u]+w+r[v])%3; 40 return false; 41 } 42 43 int main() 44 { 45 scanf("%d%d", &n, &m); 46 memset(r, 0, sizeof(r)); 47 memset(fa, -1, sizeof(fa)); 48 49 int ans = 0; 50 for(int i = 1; i<=m; i++) 51 { 52 int d, u, v; 53 scanf("%d%d%d", &d, &u, &v); 54 if(u>n || v>n) 55 ans++; 56 else if(d==2 && u==v) 57 ans++; 58 else if(Union(d-1, u, v)) 59 ans++; 60 } 61 printf("%d\n", ans); 62 }
标签:ref inpu mem 有趣 time alt opened bool scanf
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7652450.html