-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.cpp
More file actions
42 lines (40 loc) · 725 Bytes
/
Copy pathdom.cpp
File metadata and controls
42 lines (40 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n,m,p;
cin >> n>> m>>p;
int a[m],b[m],low,high,mid;
vector<int> chs = vector<int>(1,p);
for(int i=0;i<m;i++){
cin>>a[i]>>b[i];
}
int count = 1;
for (;count<=n;count++){
for (int i = 0; i < m; ++i)
{
if(b[i] == p){
p = a[i];
high = count;
low = 0;
while(low<=high){
mid = low+(high-low)/2;
if(chs[mid]==p){
cout<<-1<<endl;
exit(0);
}
if(low == high){
if(chs[low]>p)chs.insert(chs.begin()+low,p);
else chs.insert(chs.begin()+low,p);
}else if (chs[mid]<p){
low = mid+1;
} else {
low = mid-1;
}
}
break;
}
}
}
cout << count<<endl;
}