CodeForces - 645D Robot Rapping Results Report

來源:互聯網
上載者:User

標籤:section   obs   無法   spec   example   printf   space   involve   efi   

While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. 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, two robots can never have 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 two 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 two integers ui and vi (1?≤?ui,?vi?≤?nui?≠?vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots.

It is guaranteed that 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 is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.

ExamplesinputCopy
4 5
2 1
1 3
2 3
4 2
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 be (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) are possible orderings of the robots from strongest to weakest after both rap battles.

 

題目大意:輸入第一行是n和m,代表n個點m條邊,下面m行代表u為v的父親節點,求當恰好給出多少條邊時可以得到所有點的次序。

解題思路:在建樹時同時記錄以下這條邊時給出的第幾條邊,用邊權記錄。用ans記錄答案。進行拓撲排序,拓撲排序時一旦同時出現兩個及以上的0入度點,則說明有多個點的次序無法準確排序,那麼結果就是-1,否則向隊列壓入0入度點的同時記錄一下ans = max(ans, 邊權),整個拓撲排序結束後,這個ans就是所求結果。因為這樣就相當於記錄了最高次序的那個點所串連的下一個0入度點之間的邊權值,也就是最多需要給出的邊。

代碼:

 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>10 #include<stack>11 using namespace std;12 const int MaxN = 1e5;13 const int Inf = 1 << 30;14 typedef struct15 {16     int to, od;17 } Power;18 19 vector <Power> num[MaxN+5];20 int n, m, ans;21 int ind[MaxN+5];22 23 bool Toposort()24 {25     queue <int> que;26     for(int i = 1;i <= n;i++)27     {28         if(ind[i] == 0) que.push(i);29     }30     int u;31     Power v;32     while(!que.empty())33     {34         u = que.front();35         que.pop();36         if(!que.empty()) return 0;37         for(int i = 0;i < num[u].size();i++)38         {39             v = num[u][i];40             ind[v.to]--;41             if(!ind[v.to])42             {43                 ans = max(ans, v.od);44                 que.push(v.to);45             }46         }47     }48     return 1;49 }50 51 int main()52 {53     cin >> n >> m;54     int a, b;55     Power S;56     for(int i = 1;i <= m;i++)57     {58         scanf("%d %d", &a, &b);59         S.to = b;S.od = i;60         num[a].push_back(S);61         ind[b]++;62     }63     if(!Toposort()) printf("-1\n");64     else printf("%d\n", ans);65     return 0;66 }

 

 

CodeForces - 645D Robot Rapping Results Report

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.