CSES - NOI 2019 - Results
Submission details
Task:Graph Ordering
Sender:Olli Järviniemi
Submission time:2019-03-06 14:18:02 +0200
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void dfs(int)':
input/code.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0; j < g[i].size(); ++j) {
                 ~~^~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:24:2: error: 'in' was not declared in this scope
  in tn;
  ^~
input/code.cpp:24:2: note: suggested alternative: 'int'
  in tn;
  ^~
  int
input/code.cpp:25:9: error: 'n' was not declared in this scope
  cin >> n;
         ^
input/code.cpp:42:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < ans.size(); ++i) {
                  ~~^~~~~~~~~~~~

Code

#include <iostream>
#include <vector>

using namespace std;

const int N = 1e5 + 5;

vector<int> g[N];

bool z[N];

vector<int> ans;

void dfs(int i) {
	if(z[i]) return;
	z[i] = true;
	ans.push_back(i);
	for(int j = 0; j < g[i].size(); ++j) {
		dfs(g[i][j]);
	}
}

int main() {
	in tn;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
	}
	int am = 0;
	for(int i = 1; i <= n; ++i) {
		if(g[i].size() == 1) {
			++am;
			if(am == 1) {
				dfs(i);
			}
		}
	}

	if(am == 2) {
		for(int i = 0; i < ans.size(); ++i) {
			cout << ans[i] << " ";
		}
		cout << "\n";
	} else {
		cout << "IMPOSSIBLE\n";
	}


}