首页 > 学院 > 开发设计 > 正文

poj1182食物链

2019-11-14 09:12:56
字体:
来源:转载
供稿:网友

为啥就wa了呢?等过一段时间再来看看

#define N 2000+5#define NN 500000+5#define INF 0x3f3f3f3f/*****************************************************/int par[NN];int eat[NN];int ate[NN];int n;int find(int);void unite(int a, int b);int main(){ memset(eat, -1, sizeof(eat)); memset(ate, -1, sizeof(ate)); int k, cnt, temp; int flag; cnt = temp = 0; cin >> n >> k; for (int i = 0; i< n; i++){ par[i] = i; } while (k--){ int d, x, y; cin >> d >> x >> y; if (x > n || y > n){ cnt++; continue; } int m = find(x); int n = find(y); if (d == 1){ if (eat[m] == n||ate[n]==m||ate[m]==n||eat[n]==m){ cnt++; } else{ unite(n, m); n=find(n); m=find(m); } } else{ if (m == n){ cnt++; } else{ if (temp == 1){ eat[n] = flag = find(flag); ate[flag] = n; } if (eat[m] == -1 || ate[n] == -1){ if (temp == 0){ flag = m; } temp++; eat[m] = n; ate[n] = m; } else{ if (eat[m] != n || ate[n] != m) cnt++; } } } } cout << cnt << endl;}int find(int x){ if (x == par[x])return x; return par[x] = find(par[x]);}void unite(int a, int b){ a = find(a); b = find(b); if (a == b)return; par[a] = b;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表