Submission #704952

# Submission time Handle Problem Language Result Execution time Memory
704952 2023-03-03T07:13:20 Z PikaQ Naboj (COCI22_naboj) C++17
0 / 110
223 ms 34400 KB
#include<bits/stdc++.h>
#define int long long
#define forn(i,n) for(int i = 0;i < n;i++)
#define Forn(i,n) for(int i = 1;i <= n;i++)
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define all(p) p.begin(),p.end()
#define F first
#define S second
using namespace std;
void debug() {cerr << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cerr << a << " "; debug(b...);}
const int INF = 1e13 + 10;
const int MOD = (int) 998244353;
const int N = 2e5 + 9;
int n,m;
vi e1[N];
vector<pii> e2[N];
int in[N],out[N];
vector<pii> ans;
queue<int> que;
bool vis[N];
bool tag[N];
int typ[N];
void dfs(int u,int tp,int p){
//	debug(u,tp,"??");
	vis[u] = 1;
	typ[u] = tp;
	ans.pb({u,tp});
	if(tp == 1) tp = 0;
	else tp = 1;
	for(auto [c,w] : e2[u]){
		if(c == p) continue;
		if(!vis[c]){
			dfs(c,w,u);
		}
		else {
//			debug("c",w,c,typ[c]);
			if(typ[c] != w) {
				cout << "-1\n";
				exit(0);
			}
		}
	}
}


bool dfsc(int u){
	//debug(u);
	vis[u] = 1;
	tag[u] = 1;
	bool ok = 0;
	for(int c : e1[u]){
		if(tag[c]) ok = 1;
		else if(vis[c]) continue;
		else ok |= dfsc(c); 
	}
	tag[u] = 0;
	return ok;
}

void solve(){
	//pancake ( ^-^)_	
	cin >> n >> m;
	forn(i,m){
		int s,t;
		cin >> s >> t;
		out[s]++,in[t]++;
		e1[s].pb(t);
		e2[s].pb({t,1});
		e2[t].pb({s,0});
	}
	//debug("?");
	Forn(i,n) if(!vis[i]){
		if(dfsc(i)) {
			// assert(0);
			cout << "-1\n";
			return ;
		}
	}
	Forn(i,n) vis[i] = 0;
	Forn(i,n){
		if(vis[i]) continue;
		if(!vis[i] && out[i]) dfs(i,0,i);
		else if(!vis[i] && in[i]) dfs(i,1,i);
	}
	cout << ans.size() << '\n';
	forn(i,ans.size()){
		cout << ans[i].F << ' ' << ans[i].S << '\n';
	}
}

signed main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(0);
	solve();
	return 0;
}







	

Compilation message

naboj.cpp: In function 'void solve()':
naboj.cpp:3:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    3 | #define forn(i,n) for(int i = 0;i < n;i++)
......
   89 |  forn(i,ans.size()){
      |       ~~~~~~~~~~~~                 
naboj.cpp:89:2: note: in expansion of macro 'forn'
   89 |  forn(i,ans.size()){
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9716 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9636 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Incorrect 6 ms 9684 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 223 ms 34400 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9716 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9636 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Incorrect 6 ms 9684 KB Output isn't correct
9 Halted 0 ms 0 KB -