Submission #374699

#TimeUsernameProblemLanguageResultExecution timeMemory
374699gustasonFire drill (LMIO18_sauga)C++11
31.15 / 100
271 ms24044 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; set<int> adj[1005]; vector<char> col(1005, 0); void dfs(int v) { col[v] = 1; auto it = adj[v].begin(); while(it != adj[v].end()) { int u = *it; bool del = false; if (col[u] == 0) { dfs(u); } else if (col[u] == 1) { adj[v].erase(it++); del = true; } if (!del) ++it; } col[v] = 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t, n, S; cin >> t >> n >> S; for(int i = 0; i < n; i++) { int m; cin >> m; for(int j = 0; j < m; j++) { int x; cin >> x; adj[x].insert(i+1); } } for(int i = 0; i <= n; i++) { if (col[i] == 0) { dfs(i); } } vector<int> ind(n+1, 0); for(int i = 1; i <= 3; i++) { if (adj[i].empty()) continue; for(auto j : adj[i]) { ind[j]++; } } queue<int> q; for(int i = 1; i <= n; i++) { if (ind[i] == 0) { q.push(i); } } vector<int> ans; vector<bool> vis(n+1, false); while(!q.empty()) { int v = q.front(); vis[v] = true; ans.push_back(v); q.pop(); for(auto u : adj[v]) { ind[u]--; if (ind[u] == 0) { q.push(u); vis[u] = true; } } } for(int i : ans) { cout << i << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...