Submission #782546

#TimeUsernameProblemLanguageResultExecution timeMemory
782546andecaandeciNaboj (COCI22_naboj)C++17
110 / 110
511 ms16136 KiB
#include <bits/stdc++.h> using namespace std; const int lim = 200005; int degree[lim], n, m, a, b, totdeg; vector<int> adjl[lim]; vector<int> seq; bool vis[lim]; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; adjl[b].push_back(a); degree[a]++; totdeg++; } queue<int> q; for (int i = 1; i <= n; i++) { // cout << i << " " << degree[i] << endl; if (degree[i] == 0) { q.push(i); seq.push_back(i); } } // cout << totdeg << endl; while (!q.empty()) { int cur = q.front(); q.pop(); for (auto i: adjl[cur]) { degree[i]--; totdeg--; if (!vis[i]) { if (degree[i] == 0) { q.push(i); seq.push_back(i); } } } } // cout << totdeg << endl; if (totdeg > 0) cout << -1 << endl; else { cout << seq.size() << endl; for (auto i: seq) { cout << i << " " << 1 << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...