CSES - E4590 2018 6 - Results
Submission details
Task:Road network
Sender:Pohjantahti
Submission time:2018-10-20 14:54:04 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.19 sdetails
#7ACCEPTED0.19 sdetails
#8ACCEPTED0.19 sdetails
#9ACCEPTED0.19 sdetails
#10ACCEPTED0.18 sdetails
#11ACCEPTED0.02 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:65:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < vlist.size(); ++i) {
                  ~~^~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
vector<int> g[100005];
vector<int> ig[100005];

vector<int> vlist;
bool vis1[100005];

int comp[100005];

void dfs1(int s) {
	if (vis1[s]) return;
	vis1[s] = true;
	for (int a : g[s]) {
		dfs1(a);
	}
	vlist.push_back(s);
}

void dfs2(int s, int cc) {
	if (comp[s] != 0) return;
	comp[s] = cc;
	for (int a : ig[s]) {
		dfs2(a, cc);
	}
}

bool vis3[100005];

bool dfs3(int s, int tgt) {
	if (vis3[s]) return false;
	vis3[s] = true;
	if (s == tgt) {
		return true;
	}
	bool ok = false;
	for (int a : g[s]) {
		if (dfs3(a, tgt)) {
			ok = true;
		}
	}
	return ok;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		ig[b].push_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		dfs1(i);
	}
	reverse(vlist.begin(), vlist.end());
	int cc = 1;
	for (int i = 0; i < vlist.size(); ++i) {
		int cnode = vlist[i]; // joka kerta unohtuu :Dd
		if (comp[cnode] == 0) {
			dfs2(cnode, cc++);
		}
	}
	if (cc == 2) {
		cout << "YES\n";
		return 0;
	}
	int mic = 1000000005, mii = 1000000005;
	int mxc = 0, mxi = -1;
	for (int i = 1; i <= n; ++i) {
		if (comp[i] < mic) {
			mic = comp[i];
			mii = i;
		}
		if (comp[i] > mxc) {
			mxc = comp[i];
			mxi = i;
		}
	}
	cout << "NO\n";
	if (dfs3(mii, mxi)) {
		cout << mxi << " " << mii << "\n";
	}
	else {
		cout << mii << " " << mxi << "\n";
	}
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 20
8 1
9 5
6 10
6 1
...

correct output
NO
7 1

user output
NO
7 1

Test 2

Verdict: ACCEPTED

input
10 20
2 10
10 6
8 5
3 8
...

correct output
NO
1 2

user output
NO
1 2

Test 3

Verdict: ACCEPTED

input
10 20
10 2
1 5
8 3
5 6
...

correct output
NO
9 1

user output
NO
9 1

Test 4

Verdict: ACCEPTED

input
10 20
9 6
10 4
7 2
10 5
...

correct output
NO
6 1

user output
NO
6 1

Test 5

Verdict: ACCEPTED

input
10 20
5 9
10 2
3 5
7 4
...

correct output
YES

user output
YES

Test 6

Verdict: ACCEPTED

input
100000 200000
64780 62469
32706 84268
37795 14893
23995 68041
...

correct output
NO
40590 1

user output
NO
40590 1

Test 7

Verdict: ACCEPTED

input
100000 200000
74725 92399
25141 53472
70762 85785
47091 71621
...

correct output
NO
96983 1

user output
NO
96983 1

Test 8

Verdict: ACCEPTED

input
100000 200000
50342 88741
55031 42206
24989 54546
666 39964
...

correct output
NO
1 68638

user output
NO
1 68638

Test 9

Verdict: ACCEPTED

input
100000 200000
51243 54643
90493 3012
62110 9430
5809 45601
...

correct output
NO
48024 1

user output
NO
48024 1

Test 10

Verdict: ACCEPTED

input
100000 200000
5524 49109
87052 72192
46434 18442
67624 38661
...

correct output
YES

user output
YES

Test 11

Verdict: ACCEPTED

input
7 10
1 2
2 1
1 4
5 4
...

correct output
NO
2 3

user output
NO
4 3