Posts

Showing posts from April, 2019

Codeforces 510C

#include <iostream> #include <vector> using namespace std; string ret; vector<vector<int>> adj(27); bool dfs(int node,vector<bool>& vu,vector<bool>& path){ if (path[node]) return false; if (vu[node]) return true; vu[node]=true; path[node]=true; ret += 'a'+node; for (auto& i:adj[node]){ if (!dfs(i,vu,path)) return false; } path[node]=false; return true; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<string> v(n); for (int i=0; i<n; ++i) cin >> v[i]; vector<vector<int>> adj(27); for (int i=0; i<n-1; ++i){ string target=v[i],source=v[i+1]; if (target.size() > source.size() and target.substr(source.size()-1) == source){ cout << "Impossible"; return 0; } int idx=0; while (idx < target.size() and target[idx] == source[idx]) idx++; if (i...

Online Course in BSU - 770C - Codeforces

#include <bits/stdc++.h> using namespace std; const int BIG_N=100001; deque<int> ans,ret; vector<vector<int>> adj(BIG_N); bool dfs(vector<bool>& look,vector<int>& vu,int node){ if (adj[node].empty()) return true; for (auto& i:adj[node]){ if (vu[i] <= 1){ ans.emplace_back(i); vu[i]++; if (dfs(look,vu,i)) return true; } else if (look[i]) return true; else return false; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,k; cin >> n >> k; vector<int> v(k); for (int i=0; i<k; ++i) cin >> v[i]; for (int i=1; i<=n; ++i){ int t; cin >> t; for (int j=0; j<t; ++j){ int a; cin >> a; adj[i].emplace_back(a); } } vector<int> vu(n+1); vector<bool> look(n+1); for (auto& i:v){ bool ok=true; if (!vu[i]){ ok=dfs(look,vu,i); ...