Submission #699193

#TimeUsernameProblemLanguageResultExecution timeMemory
699193beepbeepsheepNaboj (COCI22_naboj)C++17
110 / 110
869 ms85216 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<long long, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ll long long #define ii pair<ll,ll> #define endl '\n' #define cerr if (0) cerr const ll inf=1e15; const ll maxn=2e5+5; const ll mod=1e9+7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll cnt[maxn][2]; bool vis[maxn]; set<ii> adj[maxn]; queue<ii> q; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n,m,a,b; cin>>n>>m; for (int i=0;i<m;i++){ cin>>a>>b; adj[a].emplace(b,0); adj[b].emplace(a,1); cnt[a][0]++; cnt[b][1]++; } for (int i=1;i<=n;i++){ cerr<<cnt[i][0]<<' '<<cnt[i][1]<<endl; if (cnt[i][0]==0){ q.emplace(i,0); vis[i]=1; } else if (cnt[i][1]==0){ q.emplace(i,1); vis[i]=1; } } stack<ii> s; while (q.size()){ tie(a,b)=q.front(); cerr<<a<<' '<<b<<'x'<<endl; q.pop(); for (auto [x,y]:adj[a]){ cerr<<a<<' '<<x<<' '<<y<<endl; adj[x].erase(make_pair(a,b)); if (cnt[x][b]==1 && vis[x]==0){ q.emplace(x,b); vis[x]=1; } cnt[x][b]--; } s.emplace(a,b); } for (int i=1;i<=n;i++){ cerr<<cnt[i][0]<<' '<<cnt[i][1]<<'y'<<endl; if (cnt[i][0] && cnt[i][1]){ cout<<-1; return 0; } } cout<<s.size()<<endl; while (s.size()){ cout<<s.top().first<<' '<<s.top().second<<endl; s.pop(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...