Title Link: Http://codeforces.com/contest/655/problem/D
The main idea is to give a few pairs of partial order, ask at least how many pairs of relationships before, can determine all the size relationship.
The solution is a two-point answer, using topological sorting to see if all relationships are uniquely determined. That is, at any one time only 1 element in the degree of 0 queue.
1#include <iostream>2#include <vector>3#include <algorithm>4#include <string>5#include <string.h>6#include <stdio.h>7#include <math.h>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A - using namespacestd; - the - Const intn=123456; - - structEdge { + intTo,next; - Edge () {} +Edge (int_to,int_nxt): to (_to), Next (_NXT) {} A} edge[n<<2]; at intidx=1, head[n]; - voidAddedge (intUintv) { -edge[++idx]=Edge (V,head[u]); -head[u]=idx; - } - int inch[n],que[n]; in - intA[n],b[n]; to + BOOLToposort (intNintmid) { -idx=1; Memset (Head,0,sizeofhead); theMemsetinch,0,sizeof inch); * for(intI=1; i<=mid;i++) { $ Addedge (A[i],b[i]);Panax Notoginseng inch[b[i]]++; - } the intTail=0; + for(intI=1; i<=n;i++) A if(inch[i]==0) theque[tail++]=i; + if(tail>1)return false; - for(intI=0; i<tail;i++){ $ intCnt=0; $ for(intk=head[que[i]];k;k=Edge[k].next) { - intv=edge[k].to; - inch[v]--; the if(inch[v]==0){ -cnt++;Wuyi if(cnt>1)return false; theque[tail++]=v; - } Wu } - } About return true; $ } - - - intMain () { A intn,m; +scanf"%d%d",&n,&m); the for(intI=1; i<=m;i++) { -scanf"%d%d", a+i,b+i); $ } the intlow=1, high=m,ret=-1; the while(low<=High ) { the intMid= (Low+high) >>1; the if(Toposort (N,mid)) { -ret=mid; inhigh=mid-1; the } the Else AboutLow=mid+1; the } theprintf"%d\n", ret); the return 0; +}
View Code
CF #CROC 2016-elimination Round D. Robot rapping Results report binary + topological ordering