UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#197034#2768. 电线snow_trace1009102ms133984kbC++113.0kb2023-11-05 10:41:452023-11-05 12:08:07

answer

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define endl '\n'
const int inf = 1000000005;
int n,m,a,b;
int in[300005],out[300005];
struct edge{
	int to,fl,id;
}e[4000005];
vector<int>p[1000005];vector<pair<int,int> >p1[300005],p2[300005];
bool can[1000005],vis[300005];
int dis[1000005],cur[1000005];int tot,s,t,head = 1;
inline void add(int a,int b,int c,int id){
	e[++head].to = b,e[head].fl = c;e[head].id = id;
	p[a].push_back(head);
	e[++head].to = a,e[head].fl = 0;e[head].id = id;
	p[b].push_back(head);return;
}
bool bfs(){
	for(int i = 1;i<=tot;i++)dis[i] = inf,cur[i] = 0;
	queue<int>q;
	q.push(s);dis[s] = 0;
	//cout << s << endl;
	while(!q.empty()){
		int now = q.front();q.pop();
		//cout << now << endl;
		for(int i =0;i<p[now].size();i++){
			int to = e[p[now][i]].to,fl = e[p[now][i]].fl;
			//	cout << " " << to << endl;
			if(!fl)continue;
			if(dis[now]+1<dis[to]){
				dis[to] = dis[now]+1;
				q.push(to);
			}
		}
	}return (dis[t]<inf);
}inline int dfs(int now,int f){
	if(now == t or !f)return f;
	int res = 0;
	for(int i = cur[now];i<p[now].size();i++){
		cur[now] = i;
		int to = e[p[now][i]].to,fl = e[p[now][i]].fl,nw = 0;
		if(!fl or dis[now]+1!=dis[to])continue;
		if(nw = dfs(to,min(f,fl))){
			res+=nw,f-=nw;
			e[p[now][i]].fl-=nw,e[p[now][i]^1].fl+=nw;
			if(!f)break;
		}
	}return res;
}int Dinic(){
	int ans = 0;
	while(bfs()){
		//	cout << 1 << endl;
		ans+=dfs(s,inf);
	}return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>a>>b;
	for(int i = 1;i<=m;i++){
		int x,y;cin >> x >> y;
		out[x]++,in[y]++;p1[x].push_back({y,i});
	}int t1 =0,t2=0;
	for(int i =1;i<=n;i++){
		if(in[i] == 0)t1++;
		if(out[i] == 0)t2++;
	}//cout << t1 << " " << t2 << endl;
	if(t1 !=1 or t2!=1 or in[a]!=0 or out[b]!=0){
		cout << "NO" << endl;return 0;
	}tot = n*2+2;s = n*2+1,t = n*2+2;
	for(int i = 1;i<=n;i++){
		if(out[i]>1)add(s,i,out[i]-1,0);
	}for(int i = 1;i<=n;i++){
		if(i!=a)add(i+n,t,1,0);
	}for(int i = 1;i<=n;i++){
		for(int j = 0;j<p1[i].size();j++){
			add(i,p1[i][j].first+n,1,p1[i][j].second);
		}
	}int res = Dinic();vector<int>ans1,ans2;
	//cout << res << endl;
	if(res!=n-1){
		cout << "NO" << endl;
	}else{
		for(int i = 3;i<=head;i+=2){
			if(e[i].id!=0 and e[i].fl!=0){
				ans1.push_back(e[i].id);can[e[i].id] = 1;
			}
		}for(int i= 1;i<=n;i++){
			for(int j = 0;j<p1[i].size();j++){
				int to = p1[i][j].first,id = p1[i][j].second;
				if(can[id])continue;
				p2[to].push_back({i,id});
			}
		}queue<int>q;q.push(b);vis[b] = 1;
		while(!q.empty()){
			int now = q.front();q.pop();
			for(int i = 0;i<p2[now].size();i++){
				int to = p2[now][i].first,id = p2[now][i].second;
				if(vis[to] == 0){
					vis[to] = 1;ans2.push_back(id);q.push(to);
				}
			}
		}cout << "YES" << endl;
		for(int i =0;i<ans1.size();i++)cout << ans1[i] << ' ';cout << endl;
		for(int i =0;i<ans2.size();i++)cout << ans2[i] << ' ';cout << endl;
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 30
Accepted

Test #1:

score: 30
Accepted
time: 20ms
memory: 38816kb

input:

20 47 1 20
1 2
1 3
3 4
3 5
1 6
6 7
5 8
3 9
9 10
3 11
8 12
12 13
11 14
10 15
8 16
10 17
1 18
10 19
2 ...

output:

YES
1 2 5 17 20 19 21 3 8 41 7 24 6 11 15 28 14 29 12 
31 35 37 38 30 32 44 25 34 18 36 10 26 23 27 ...

result:

ok ok

Test #2:

score: 0
Accepted
time: 3ms
memory: 38816kb

input:

20 54 1 20
1 2
1 3
1 4
2 5
5 6
2 7
3 8
6 9
4 10
6 11
6 12
8 13
7 14
9 15
9 16
4 17
2 18
12 19
9 20
1...

output:

YES
1 2 3 4 6 17 21 7 39 40 9 16 23 45 24 8 12 19 46 
52 35 37 38 13 41 30 31 32 50 29 36 22 42 53 4...

result:

ok ok

Test #3:

score: 0
Accepted
time: 4ms
memory: 38816kb

input:

20 53 1 20
1 2
1 3
2 4
2 5
4 6
4 7
3 8
1 9
5 10
4 11
5 12
10 13
11 14
7 15
12 16
6 17
8 18
13 19
10 ...

output:

YES
1 2 8 20 3 4 21 40 7 22 6 10 23 42 53 24 25 17 28 
27 19 38 9 39 26 44 18 50 37 48 5 13 31 34 43...

result:

ok ok

Test #4:

score: 0
Accepted
time: 4ms
memory: 38812kb

input:

20 58 1 20
1 2
1 3
1 4
2 5
5 6
4 7
3 8
4 9
2 10
5 11
6 12
3 13
1 14
3 15
7 16
9 17
11 18
6 19
13 20
...

output:

YES
1 2 3 13 20 4 9 21 55 7 12 14 42 8 23 10 11 15 58 
51 29 19 33 36 38 41 52 48 43 35 53 24 18 37 ...

result:

ok ok

Test #5:

score: 0
Accepted
time: 4ms
memory: 38812kb

input:

20 51 1 20
1 2
1 3
1 4
4 5
3 6
2 7
3 8
3 9
3 10
7 11
5 12
3 13
13 14
11 15
12 16
1 17
12 18
14 19
6 ...

output:

YES
1 2 3 16 6 5 7 8 9 12 4 23 11 10 46 29 41 14 48 
47 24 19 27 36 37 38 49 20 35 42 17 32 18 34 51...

result:

ok ok

Test #6:

score: 0
Accepted
time: 3ms
memory: 38740kb

input:

20 59 1 20
11 18
17 20
8 12
2 18
16 19
14 20
3 12
7 10
1 6
13 19
15 16
17 19
5 20
19 20
4 14
3 13
8 ...

output:

NO

result:

ok ok

Subtask #2:

score: 30
Accepted

Test #7:

score: 30
Accepted
time: 19ms
memory: 38904kb

input:

300 874 1 300
1 2
1 3
3 4
3 5
4 6
3 7
6 8
5 9
7 10
10 11
3 12
2 13
1 14
4 15
1 16
12 17
7 18
13 19
1...

output:

YES
1 2 13 15 27 112 279 281 300 774 12 763 820 3 4 6 11 31 36 62 295 302 5 14 44 89 303 602 8 30 68...

result:

ok ok

Test #8:

score: 0
Accepted
time: 7ms
memory: 38892kb

input:

300 626 1 300
1 2
2 3
1 4
1 5
1 6
1 7
5 8
3 9
8 10
5 11
2 12
11 13
4 14
6 15
2 16
9 17
8 18
11 19
11...

output:

YES
1 3 4 5 6 55 78 177 236 2 11 15 35 68 213 604 8 25 89 13 51 172 7 10 77 105 14 33 80 285 22 40 4...

result:

ok ok

Test #9:

score: 0
Accepted
time: 4ms
memory: 38896kb

input:

300 772 1 300
1 2
2 3
1 4
1 5
4 6
6 7
4 8
5 9
4 10
9 11
8 12
4 13
12 14
14 15
7 16
7 17
10 18
10 19
...

output:

YES
1 3 4 27 62 64 74 100 132 300 2 175 213 301 32 122 272 5 7 9 12 20 22 215 303 613 8 21 28 53 85 ...

result:

ok ok

Test #10:

score: 0
Accepted
time: 8ms
memory: 38900kb

input:

300 805 1 300
1 2
2 3
2 4
4 5
3 6
5 7
6 8
8 9
1 10
4 11
11 12
6 13
1 14
8 15
5 16
3 17
2 18
16 19
13...

output:

YES
1 9 13 54 58 147 300 2 3 17 21 255 301 5 16 20 24 101 140 4 10 6 15 26 125 285 304 7 12 25 48 59...

result:

ok ok

Test #11:

score: 0
Accepted
time: 3ms
memory: 38892kb

input:

300 649 1 300
1 2
1 3
3 4
1 5
2 6
4 7
4 8
7 9
7 10
5 11
11 12
9 13
8 14
1 15
15 16
9 17
12 18
3 19
1...

output:

YES
1 2 4 14 55 84 123 154 5 42 47 102 108 132 143 277 3 18 25 37 40 67 200 293 302 6 7 23 41 51 74 ...

result:

ok ok

Test #12:

score: 0
Accepted
time: 7ms
memory: 38896kb

input:

300 761 1 300
1 2
1 3
1 4
3 5
2 6
4 7
6 8
2 9
9 10
5 11
1 12
6 13
4 14
11 15
3 16
11 17
3 18
15 19
2...

output:

YES
1 2 3 11 29 52 200 300 639 5 8 19 21 59 76 77 82 139 232 261 746 749 4 15 17 25 51 210 6 13 24 3...

result:

ok ok

Test #13:

score: 0
Accepted
time: 3ms
memory: 38900kb

input:

300 857 1 300
1 2
1 3
3 4
1 5
5 6
3 7
5 8
1 9
3 10
8 11
2 12
7 13
8 14
8 15
11 16
2 17
11 18
1 19
8 ...

output:

YES
1 2 4 8 18 26 50 167 233 705 749 826 11 16 22 203 301 3 6 9 68 171 234 235 302 600 77 303 634 5 ...

result:

ok ok

Test #14:

score: 0
Accepted
time: 4ms
memory: 38900kb

input:

300 825 1 300
1 2
1 3
1 4
1 5
5 6
1 7
7 8
5 9
8 10
5 11
4 12
1 13
5 14
3 15
13 16
1 17
4 18
16 19
12...

output:

YES
1 2 3 4 6 12 16 300 680 80 296 301 694 14 60 105 302 658 695 11 17 24 26 303 5 8 10 13 43 61 65 ...

result:

ok ok

Test #15:

score: 0
Accepted
time: 8ms
memory: 38908kb

input:

300 885 1 300
1 2
1 3
3 4
1 5
4 6
1 7
1 8
4 9
6 10
7 11
1 12
12 13
1 14
11 15
6 16
7 17
3 18
13 19
1...

output:

YES
1 2 4 6 7 11 13 181 43 105 3 17 129 145 302 5 8 25 37 192 202 303 641 702 736 794 30 59 132 228 ...

result:

ok ok

Test #16:

score: 0
Accepted
time: 4ms
memory: 38892kb

input:

300 625 1 300
1 2
1 3
2 4
3 5
4 6
6 7
3 8
3 9
3 10
9 11
3 12
9 13
1 14
12 15
6 16
13 17
5 18
5 19
17...

output:

YES
1 2 13 58 111 134 3 67 158 242 4 7 8 9 11 27 221 5 20 37 180 265 17 18 106 185 187 304 6 15 51 4...

result:

ok ok

Test #17:

score: 0
Accepted
time: 7ms
memory: 38756kb

input:

300 812 1 300
6 184
116 247
27 150
20 132
87 288
16 233
117 193
64 168
187 230
151 211
99 167
70 225...

output:

NO

result:

ok ok

Subtask #3:

score: 40
Accepted

Test #18:

score: 40
Accepted
time: 561ms
memory: 114720kb

input:

300000 604072 1 300000
1 2
2 3
3 4
1 5
1 6
6 7
4 8
2 9
4 10
3 11
3 12
10 13
9 14
10 15
12 16
5 17
8 ...

output:

YES
1 4 5 40 148 380 3041 4842 13823 43508 56930 87966 259658 2 8 25 59 140 196 5947 9056 10982 2689...

result:

ok ok

Test #19:

score: 0
Accepted
time: 973ms
memory: 132248kb

input:

300000 872432 1 300000
1 2
1 3
3 4
4 5
5 6
4 7
1 8
3 9
7 10
7 11
4 12
4 13
6 14
14 15
1 16
12 17
13 ...

output:

YES
1 2 7 15 23 1134 1538 2087 2316 20717 33517 126303 131726 300000 670498 689040 206 465 10421 158...

result:

ok ok

Test #20:

score: 0
Accepted
time: 647ms
memory: 116308kb

input:

300000 631604 1 300000
1 2
1 3
2 4
2 5
5 6
5 7
6 8
2 9
7 10
3 11
6 12
12 13
9 14
1 15
3 16
2 17
10 1...

output:

YES
1 2 14 34 600 706 796 879 3709 68075 3 4 8 16 20 28 42 107 324 578 657 8891 10644 25324 123230 3...

result:

ok ok

Test #21:

score: 0
Accepted
time: 880ms
memory: 128016kb

input:

300000 807601 1 300000
1 2
1 3
3 4
3 5
4 6
5 7
3 8
4 9
9 10
10 11
1 12
3 13
2 14
1 15
3 16
3 17
8 18...

output:

YES
1 2 11 14 23 25 34 803 934 1032 6796 32524 68639 251365 300000 768882 13 52 2831 25290 50385 300...

result:

ok ok

Test #22:

score: 0
Accepted
time: 635ms
memory: 115868kb

input:

300000 622368 1 300000
1 2
2 3
3 4
1 5
3 6
6 7
3 8
2 9
7 10
7 11
7 12
9 13
5 14
2 15
13 16
8 17
15 1...

output:

YES
1 4 21 204 380 455 840 4426 73204 87852 2 8 14 41 169 666 686 33754 155989 300001 3 5 7 20 419 8...

result:

ok ok

Test #23:

score: 0
Accepted
time: 963ms
memory: 131036kb

input:

300000 855374 1 300000
1 2
2 3
2 4
4 5
1 6
5 7
4 8
4 9
2 10
8 11
3 12
6 13
7 14
7 15
2 16
5 17
14 18...

output:

YES
1 5 31 35 64 81 207 979 42505 60274 300000 660222 695182 815421 2 3 9 15 330 348 372 1518 2474 4...

result:

ok ok

Test #24:

score: 0
Accepted
time: 1206ms
memory: 133372kb

input:

300000 892420 1 300000
1 2
1 3
1 4
2 5
4 6
1 7
2 8
2 9
4 10
1 11
3 12
1 13
6 14
1 15
2 16
14 17
2 18...

output:

YES
1 2 3 6 10 12 14 183 256 286 673 701 4185 20143 31468 59637 300000 4 7 8 15 17 20 23 67 455 2371...

result:

ok ok

Test #25:

score: 0
Accepted
time: 1181ms
memory: 126972kb

input:

300000 793789 1 300000
1 2
2 3
3 4
4 5
1 6
4 7
7 8
4 9
5 10
2 11
2 12
8 13
7 14
13 15
12 16
8 17
17 ...

output:

YES
1 5 324 450 1344 2650 21320 273661 300000 688675 2 10 11 61 79 520 811 852 1602 1904 2052 23152 ...

result:

ok ok

Test #26:

score: 0
Accepted
time: 704ms
memory: 118672kb

input:

300000 663937 1 300000
1 2
2 3
2 4
4 5
1 6
6 7
1 8
2 9
7 10
6 11
2 12
2 13
1 14
9 15
8 16
10 17
14 1...

output:

YES
1 5 7 13 48 267 763 873 1822 4419 50439 77923 79820 86781 154185 300000 2 3 8 11 12 33 1815 2112...

result:

ok ok

Test #27:

score: 0
Accepted
time: 1022ms
memory: 133984kb

input:

300000 898680 1 300000
1 2
2 3
2 4
2 5
1 6
5 7
4 8
8 9
8 10
6 11
5 12
1 13
1 14
14 15
10 16
8 17
17 ...

output:

YES
1 5 12 13 18 33 88 158 286 573 11121 115331 133595 154407 300000 2 3 4 837 2577 5923 8929 11862 ...

result:

ok ok

Test #28:

score: 0
Accepted
time: 218ms
memory: 52016kb

input:

300000 662293 1 300000
162581 234886
65634 213492
35056 256016
163805 203054
59196 128726
139601 169...

output:

NO

result:

ok ok

Extra Test:

score: 0
Extra Test Passed