Cod sursa(job #2236237)

Utilizator pas.andreiPopovici Andrei-Sorin pas.andrei Data 28 august 2018 19:24:27
Problema Party Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <bits/stdc++.h>
#define NMAX 105
#define INF 0x3f3f3f3f
#define MOD 1000003
#define pb push_back
#define x first
#define y second

using namespace std;

ifstream fin("party.in");
ofstream fout("party.out");

int n;
stack<int> st;
vector<int> G[NMAX], GT[NMAX];
bool viz[NMAX], stare[NMAX], ok;

inline int negat(int x) {
	if(x<=n) return x+n;
	return x-n;
}

void DFSp(int x) {
	viz[x] = 1;

	for(auto it:G[x])
		if(!viz[it]) DFSp(it);
	st.push(x);
}

void DFSm(int x) {
	viz[x] = 0;

	if(x > n) stare[negat(x)] = 1;

	for(auto it:GT[x])
		if(viz[it]) DFSm(it);
}

int main() {
	int m,i,x,y,t;

	fin>>n>>m;
	for(i=1;i<=m;++i) {
		fin>>x>>y>>t;

		if(t == 1) y = negat(y);
		else if(t == 2) x = negat(x);
		else if(t == 3) x = negat(x), y = negat(y);

		G[negat(x)].pb(y);
		G[negat(y)].pb(x);

		GT[x].pb(negat(y));
		GT[y].pb(negat(x));
	}

	for(i=1;i<=2*n;++i)
		if(!viz[i])
			DFSp(i);

	while(!st.empty()) {
		if(viz[st.top()] && viz[negat(st.top())])
			DFSm(st.top());
		st.pop();
	}

	for(i=1;i<=2*n;++i)
		if(stare[i]) fout << i << '\n';

	return 0;
}