52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
// Flight Routes Check
|
|
|
|
#include<bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
void dfs(bitset<(int)1e5>& v, int s, vector<vector<int>> &g) {
|
|
v[s] = true;
|
|
for (auto &e: g[s]) {
|
|
if (!v[e]) {
|
|
dfs(v, e, g);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int n, m; cin >> n >> m;
|
|
vector<vector<int>> g(n);
|
|
vector<vector<int>> r(n);
|
|
for (int i{}; i < m; i++) {
|
|
int a, b; cin >> a >> b; a--; b--;
|
|
g[a].push_back(b);
|
|
r[b].push_back(a);
|
|
}
|
|
bitset<(int)1e5> v = 0;
|
|
dfs(v, 0, g);
|
|
bitset<(int)1e5> v2 = 0;
|
|
if (v.count() != n) {
|
|
cout << "NO" << endl;
|
|
for (int i{}; i < n; i++) {
|
|
if (!v[i]) {
|
|
cout << 1 << " " << i + 1 << endl;
|
|
return 0;
|
|
}
|
|
}
|
|
} else {
|
|
dfs(v2, 0, r);
|
|
if (v2.count() != n) {
|
|
cout << "NO" << endl;
|
|
for (int i{}; i < n; i++) {
|
|
if (!v2[i]) {
|
|
cout << i + 1 << " " << 1 << endl;
|
|
return 0;
|
|
}
|
|
}
|
|
} else {
|
|
cout << "YES" << endl;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|