Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怎么都找不到错误啊,求帮忙,我用的单链表实现带权并查集#include<iostream> using namespace std; class Node { friend class FoodChain; friend int main(); private: int head; int next; int relation; int size; }; class FoodChain { friend int main(); public: FoodChain(int N); ~FoodChain(); int Find(int i); void Union(int d,int i, int j); private: int number; Node *table; }; FoodChain::FoodChain(int N) { number = N; table = new Node [number+1]; for (int i= 1; i <= number; i++) { table[i].head = i; table[i].next = 0; table[i].relation = 0; table[i].size = 1; } } FoodChain::~FoodChain() { delete[] table; } int FoodChain::Find(int i) { return table[i].head; } void FoodChain::Union(int d,int i, int j) { int a = Find(i); int b = Find(j); if (table[a].size >= table[b].size) { Node *p = &table[b]; p->head = a; table[a].size += p->size; p->size = 1; p->relation = (3 - table[j].relation + d - 1 + table[i].relation) % 3; while (p->next != 0) { p = &table[p->next]; p->head = a; p->relation = (p->relation + table[b].relation) % 3; } p->next = table[a].next; table[a].next = b; } else { Node *p = &table[a]; p->head = b; table[b].size += p->size; p->size = 1; p->relation = (- table[i].relation + 1-d + table[j].relation) % 3; while (p->next != 0) { p = &table[p->next]; p->head = b; p->relation = (p->relation + table[a].relation) % 3; } p->next = table[b].next; table[b].next = a; } } int main() { int N, K, count = 0; cin >> N >> K; int **p = new int *[K+1]; for (int i = 1; i <= K; i++) { p[i] = new int[3]; for (int j = 0; j < 3; j++) { cin >> p[i][j]; } } FoodChain chain(N); for (int i = 1; i <= K; i++) { if (p[i][1]>N || p[i][2]>N || (p[i][0] ==2 && p[i][1] == p[i][2])) { count++; continue; } if (p[i][0] == 1) { if (chain.Find(p[i][1]) != chain.Find(p[i][2])) { chain.Union(p[i][0], p[i][1], p[i][2]); } else { if (chain.table[p[i][1]].relation != chain.table[p[i][2]].relation)count++; } } else if (p[i][0] == 2) { if (chain.Find(p[i][1]) != chain.Find(p[i][2])) { chain.Union(p[i][0], p[i][1], p[i][2]); } else { if ((chain.table[p[i][2]].relation + (3 - chain.table[p[i][1]].relation)) % 3 != 1)count++; } } } cout << count << endl;; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator