CF #CROC 2016-elimination Round D. Robot rapping Results report binary + topological ordering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.