Submission #781596

#TimeUsernameProblemLanguageResultExecution timeMemory
781596andecaandeciNaboj (COCI22_naboj)C++17
0 / 110
384 ms25676 KiB
#include <bits/stdc++.h> #define int long long #define pb push_back #define pii pair<int, int> #define uh pair<int,pii> #define fr first #define sc second #define all(x) x.begin(), x.end() using namespace std; const int N = 2e5+5; const int M = 5e5+5; vector<int> adj[N]; vector<int> opp[N]; vector<int> marker; vector<int> par; vector<bool> sequenced; vector<vector<int>> ano; bool dfs(int x) { marker[x] = 1; for (auto item : adj[x]) { if (!marker[item]) { par[item] = x; if (dfs(item)) return true; } else if (marker[item] == 1) return true; } marker[x] = 2; return false; } signed main() { int n, m; cin >> n >> m; marker.assign(n+1, 0); par.assign(n+1, -1); sequenced.assign(n+1, 0); // if going forward then for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[b].pb(a); opp[a].pb(b); } for (int i = 1; i <= n; i++) { if (!marker[i]) { if (dfs(i)) { cout << -1 << endl; return 0; } } } for (int i = 1; i <= n; i++) { if (opp[i].size() and adj[i].size() and !sequenced[i]) { vector<int> v; queue<int> q; v.pb(i); q.push(i); while(!q.empty()) { int cur = q.front(); q.pop(); sequenced[cur] = 1; for (auto item : opp[cur]) { if (!sequenced[item] and opp[item].size()) { q.push(item); v.pb(item); } } } reverse(v.begin(), v.end()); q.push(i); while(!q.empty()) { int cur = q.front(); q.pop(); sequenced[cur] = 1; for (auto item : adj[cur]) { if (!sequenced[item] and adj[item].size()) { q.push(item); v.pb(item); } } } ano.pb(v); } } cout << n << endl; for (auto vec : ano) { for (auto item : vec) cout << item << " " << 1 << endl; } for (int i = n; i >= 1; i--) { if (!sequenced[i]) { cout << i << " " << (adj[i].size() == 0) << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...