Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- typedef long long ll;
- using namespace std;
- ll tests=0, keycount=0, hintcount=0;
- int zeros=0;
- int d[10002];
- int c[10002]; // for cycles
- vector<int> k[10002];
- queue<int> order;
- vector<int> sequence;
- stack<int> unvisited;
- set<int> visiting;
- set<int> visited;
- void doTopo(){
- int maxQueueSize=0;
- int tmp_keys=keycount;
- while(!order.empty()){
- int cur = order.front();
- order.pop();
- tmp_keys--;
- sequence.push_back(cur);
- for(int i=0;i<k[cur].size();i++){
- d[k[cur][i]]--;
- if(d[k[cur][i]]==0){
- order.push(k[cur][i]);
- }
- }
- maxQueueSize=max(maxQueueSize,(int)order.size());
- }
- if(zeros==1 && tmp_keys==0 && maxQueueSize==1){
- for(int i=0;i<sequence.size();i++){
- cout << sequence[i] << " ";
- }
- }else{
- cout << "missing hints";
- }
- }
- bool dfs(int v){
- visiting.insert(v);
- for(int c=0;c<k[v].size();c++){
- int child = k[v][c];
- if(visited.find(child)!=visited.end())continue;
- if(visiting.find(child)!=visiting.end())return true;
- if(dfs(child))return true;
- }
- visiting.erase(v);
- visited.insert(v);
- return false;
- }
- bool findCycle(){
- while(!unvisited.empty()){
- int cur = unvisited.top();
- unvisited.pop();
- if(dfs(cur)){
- return true;
- }
- }
- return false;
- }
- int main()
- {
- ifstream cin("data.in");
- cin >> tests;
- for(int te=0;te<tests;te++){
- keycount=0;
- hintcount=0;
- sequence.clear();
- for(int i=0;i<=10002;i++){
- d[i]=0;
- c[i]=-1;
- k[i].clear();
- }
- while(!order.empty()){order.pop();}
- while(!unvisited.empty()){unvisited.pop();}
- visiting.clear();
- visited.clear();
- cin >> keycount >> hintcount;
- if(hintcount == 0){
- cout << "missing hints";
- continue;
- }
- for(int i=1;i<=keycount;i++){
- unvisited.push(i);
- }
- for(int i=0;i<hintcount;i++){
- int a,b;
- cin >> a >> b; // a comes before b
- d[b]+=1;
- k[a].push_back(b);
- }
- zeros = 0;
- for(int i=1;i<=keycount;i++){
- if(d[i]==0){
- zeros+=1;
- order.push(i);
- }
- }
- if(findCycle()){
- cout << "recheck hints";
- }else{
- doTopo();
- }
- if(te!=tests-1) cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement