This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, m, k;
int indegree[N], ans[N];
vector<int> adj[N];
vector<int> topo;
queue <int> listSource;
void process() {
cin >> n >> m;
foru(i,1,m) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
indegree[v]++;
}
for (int u = 1; u <= n; ++u)
if (!indegree[u]) listSource.push(u);
while (!listSource.empty()) {
int u = listSource.front();
listSource.pop();
topo.push_back(u);
for (auto v : adj[u]) {
indegree[v]--;
if (!indegree[v]) listSource.push(v);
}
}
if (topo.size() < n) {
cout << "-1";
return;
}
cout << topo.size() << '\n';
for (auto x: topo) cout << x << ' ' << 0 << '\n';
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
Compilation message (stderr)
naboj.cpp: In function 'void process()':
naboj.cpp:47:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
47 | if (topo.size() < n) {
| ~~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |