codeforces-645d Robot rapping Results report

Source: Internet
Author: User

While Farmer John rebuilds he farm in an unfamiliar portion of Bovinia, Bessie was out trying some alternative jobs. In She new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the Robot Rap Battle Tournament, she notices it all of the robots operate under deterministic Algor Ithms. In particular, robot i  will beat Robot  J  if and only if Robot  i  has a higher skill level than Robot  J . And if Robot  i  beats robot  J  and robot  J  beats robot  K , then Robot  i  will beat Robot  K . Since rapping is such a subtle art, and the robots can never has the same skill level.

Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap Battles that needed to take place before Bessie could order all of the robots by skill level.

Input

The first line of the input consists of integers, the number of robots n (2?≤? n? ≤?100?000) and the number of rap battles M ().

The next m  lines describe the results of the rap battles in the order they took place. Each consists of Integers  u i  and   v i   (1?≤? U i ,? V i ? ≤? n ,   u i ? ≠? V i ), indicating that Robot  u i  beat robot  v i  in the  I -th rap battle. No rap battles involve the same pair of robots.

It is guaranteed so at least one ordering of the robots satisfies all m relations.

Output

Print the minimum K such that the ordering of the robots by skill level was uniquely defined by the first k Rap battles. If there exists more than one ordering this satisfies all m relations, output -1.

Examplesinputcopy
4 5
2 1
1 3
2 3
5 S
4 3
Outputcopy
4
Inputcopy
3 2
1 2
3 2
Outputcopy
-1
Note

In the first sample, the robots from strongest to weakest must is (4,?2,?1,?3), which Bessie can deduce after knowing The results of the first four rap battles.

In the second sample, both (1,?3,?2) and (3,?1,?2) is possible orderings of the robots from strongest to weakest After both rap battles.

Main topic: Input the first line is N and M, representing n points M edge, the following m line represents the Father node of V, you can get the order of all points when exactly how many edges are given.

Problem-solving ideas: in the achievement of the same time record the following this edge when the first side, with the edge of the right record. Use ANS to record answers. Topological sorting, once the topology of two or more at the same time when the order of 0 points, then the sequence of multiple points can not be accurately sorted, then the result is-1, otherwise, the queue into the 0-point to record the same time ans = max (ans, edge), the entire topology after the end of the order, this ans is the result. This is equivalent to recording the Benquan value between the next 0-point points connected by the highest order point, which is the maximum number of edges to be given.

Code:

1#include <cstdio>2#include <cstdlib>3#include <cmath>4#include <cstring>5#include <iostream>6#include <algorithm>7#include <string>8#include <vector>9#include <queue>Ten#include <stack> One using namespacestd; A Const intMAXN =1e5; - Const intINF =1<< -; -typedefstruct the { -     intto, OD; - } Power; -  +Vector <Power> num[maxn+5]; - intN, m, ans; + intind[maxn+5]; A  at BOOLToposort () - { -Queue <int>que; -      for(inti =1; I <= n;i++) -     { -         if(Ind[i] = =0) Que.push (i); in     } -     intu; to Power v; +      while(!que.empty ()) -     { theU =Que.front (); * Que.pop (); $         if(!que.empty ())return 0;Panax Notoginseng          for(inti =0; i < num[u].size (); i++) -         { thev =Num[u][i]; +ind[v.to]--; A             if(!ind[v.to]) the             { +Ans =Max (ans, v.od); - Que.push (v.to); $             } $         } -     } -     return 1; the } - Wuyi intMain () the { -CIN >> N >>m; Wu     intA, B; - Power S; About      for(inti =1; I <= m;i++) $     { -scanf"%d%d", &a, &b); -S.to = b; S.od =i; - Num[a].push_back (S); Aind[b]++; +     } the     if(! Toposort ()) printf ("-1\n"); -     Elseprintf"%d\n", ans); $     return 0; the}

codeforces-645d Robot rapping Results report

Related Article

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.