Links: http://acm.hdu.edu.cn/showproblem.php?pid=1811
Chinese question. Check that the given relationship is contradictory, or not unique, or correct.
Background: WA for a long time .... Before the idea has been not very clear, later thinking better to pay or wrong, the results of the week. And then dragged on for a few days, this morning only to take it out to write, and WA, feel fast collapse, all want to search the problem. Still endure. Later found conflict and not only exist words to output conflict ... Check, not the only I went straight back to orzzzzzz changed, finally a .... It's almost crying.
Idea: At the same time as the input for each "=" to the two sides of the number is merged into a set, if you can merge to the number of orders need to be reduced by 1, the last input at the end can be more than one collection, of course, there is only one element of the collection, traverse all operations, build, establish the relationship between all sets (Each collection is represented by its own root node), and all collections are topologically sorted (also represented by the root node of each collection).
To be clear is that at any time if the queue is size>1 then this topology sort is not unique, and if the number of elements entering the queue is less than the number of elements to be sorted, then there is a loop that is conflicting.
Be careful not to return immediately because it may be a conflict.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Vector> #include <queue>using namespace std; #define M 10009vector<int> g[m];int in[m];int p[m];int n,m; int Num;int A[2*m],b[2*m];char c[2*m];bool flag;int Find (int x)//finds root node and makes path compression {return X==p[x]?x:p[x]=find (p[x]);} void Uni (int x,int y) {x = find (x); y = find (y); if (x!=y) {p[y] = x; num--; If you can merge, reduce the number of orders required by 1}}int toposort () {queue<int> q; for (int i = 0;i < n;i++) {if (!in[i] && p[i]==i)//Enter at 0 and the root node is pressed into the queue q.push (i); } while (!q.empty ()) {int cur = q.front (); if (Q.size () >1) flag = true; !! Be careful not to return 0 because in the case of conflict and incomplete, it is necessary to output the conflict q.pop (); num--; for (int i = 0;i < G[cur].size (); i++) {int temp = G[cur][i]; in[temp]--; if (!in[temp]) Q.push (temp); }} if (num>0) return-1; If(flag) return 0; return 1;} int main () {while (scanf ("%d%d", &n,&m) ==2) {num = n; Flag = false; memset (In,0,sizeof (in)); for (int i = 0;i < n;i++) {g[i].clear ();//empty vector p[i] = i;//Initialize parent node} for (int i = 0;i < m;i++) {scanf ("%d%c%d", &a[i],&c[i],&b[i]); if (c[i]== ' = ') {uni (a[i],b[i]); }} for (int i = 0;i < m;i++) {if (c[i]== ' < ') {int x = fin D (A[i]); Find the root node int y = Find (b[i]); G[x].push_back (y); operate on the root node. in[y]++; } if (c[i]== ' > ') {int x = find (B[i]); int y = find (a[i]); G[x].push_back (y); in[y]++; }} int ok = Toposort (); if (!ok) printf ("uncertain\n"); else if (ok==-1) PRintf ("conflict\n"); else if (ok==1) printf ("ok\n"); } return 0;}
Attached to the blog written by others feel good to write: http://972169909-qq-com.iteye.com/blog/1052820
HDU 1811 and search set + topology sequencing