Submission #792322

#TimeUsernameProblemLanguageResultExecution timeMemory
792322cig32Naboj (COCI22_naboj)C++17
110 / 110
170 ms33828 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int MAXN = 2e5 + 10; const int MOD = 1e9 + 7; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x, int y) { int u = uniform_int_distribution<int>(x, y)(rng); return u; } int bm(int b, int p) { if(p==0) return 1 % MOD; int r = bm(b, p >> 1); if(p&1) return (((r*r) % MOD) * b) % MOD; return (r*r) % MOD; } int inv(int b) { return bm(b, MOD-2); } int fastlog(int x) { return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1); } void printcase(int i) { cout << "Case #" << i << ": "; } int n, m; vector<int> adj[MAXN]; int K[MAXN]; bool vis[MAXN]; stack<int> topsort; void dfs(int node) { vis[node] = 1; for(int x: adj[node]) { if(!vis[x]) { dfs(x); } } topsort.push(node); } void solve(int tc) { cin >> n >> m; vector<pair<int, int> > constraints; for(int i=1; i<=m; i++) { int a, b; cin >> a >> b; constraints.push_back({a, b}); adj[b].push_back(a); } for(int i=1; i<=n; i++) { if(!vis[i]) { dfs(i); } } for(int i=1; i<=n; i++) { K[topsort.top()] = i; topsort.pop(); } for(pair<int, int> X: constraints) { if(K[X.first] < K[X.second]) { cout << "-1\n"; return; } } int pos[n+1]; for(int i=1; i<=n; i++) pos[K[i]] = i; cout << n << "\n"; for(int i=1; i<=n; i++) { cout << pos[i] << " 1\n"; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; for(int i=1; i<=t; i++) solve(i); } // 勿忘初衷
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...