2522021-03-06 16:39:28mraronEgyirányú egyensúlycpp14Accepted 50/50246ms21980 KiB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

using ll = long long;
using ull = unsigned long long ;
using ld = long double ;
using str = string;
using ordered_set=tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)


struct EulerPath {
	int n,eid;
	bool dir;
	vector<vector<pair<int,int>>> adj;
	vector<int> indeg, outdeg;
	EulerPath(int n_, bool dir_=false) : n(n_), eid(0), dir(dir_), adj(n_+1), indeg(n_+1), outdeg(n_+1) {
		
	}
	
	void add_edge(int a, int b) {
		adj[a].push_back({b,eid});outdeg[a]++;indeg[b]++;
		if(!dir) adj[b].push_back({a,eid});
		eid++;
	}
	
	vector<int> find(int from) {
		bool ok=true;
		if(!dir) {
			int cnt=0;
			int is_from=0;
			for(int i=1;i<=n;++i) {
				if(adj[i].size()&1) {
					cnt++;
					if(i==from) is_from=1;
				}
			}
			
			if(cnt==0) ok=true;
			else if(cnt==2) {
				ok=is_from;
			}else {
				ok=false;
			}
		}else {
			vector<int> st,en;
			for(int i=1;i<=n;++i) {
				if(indeg[i]!=outdeg[i]) {
					if(abs(indeg[i]-outdeg[i])!=1) ok=false;
					else if(indeg[i]>outdeg[i]) {
						en.push_back(i);
					}else {
						st.push_back(i);
					}
				}
			}
			
			ok&=(st.size()==1 && st[0]==from && en.size()==1) || st.size()+en.size()==0;
		}
		
		if(!ok) return {};
		
		vector<int> volt(eid);
		vector<int> ord;
		
		stack<int> st;
		st.push(from);
		
		while(!st.empty()) {
			int x=st.top(); 
			while(!adj[x].empty() && volt[adj[x].back().second]) adj[x].pop_back();
			
			if(!adj[x].empty()) {
				volt[adj[x].back().second]=1;
				st.push(adj[x].back().first);
			}else {
				ord.push_back(x);
				st.pop();
			}
		}
		
		reverse(ord.begin(), ord.end());
		
		return ord;
	}
};

int n,m;
vector<pair<int,int>> edgs;
vector<int> deg, indeg, outdeg;

pair<int,int> index(pair<int,int> x) {
	return {min(x.xx, x.yy), max(x.xx,  x.yy)};
}

int main() {
	IO;
	cin>>n>>m;
	edgs.resize(m);
	deg.resize(n+1);
	indeg.resize(n+1);
	outdeg.resize(n+1);
	EulerPath ep(n+1);
	for(int i=0;i<m;++i) {
		cin>>edgs[i].xx>>edgs[i].yy;
		deg[edgs[i].xx]++;
		deg[edgs[i].yy]++;
		ep.add_edge(edgs[i].xx, edgs[i].yy);
	}
	
	int odd=n+1;
	for(int i=1;i<=n;++i) {
		if(deg[i]%2==1) ep.add_edge(odd, i);
	}
	
	vector<int> volt(n+1);
	map<pair<int,int>, vector<int>> lst;

	for(int st=1;st<=n;++st) {
		if(volt[st]) continue ;

		vector<int> res=ep.find(st);
		for(int i=0;i+1<sz(res);++i){
			if(res[i+1]<=n && res[i]<=n) {
				volt[res[i]]=1;
				volt[res[i+1]]=1;
				
				indeg[res[i+1]]++;
				outdeg[res[i]]++;

				lst[index(mp(res[i], res[i+1]))].push_back(index(mp(res[i], res[i+1]))==mp(res[i], res[i+1]));
			}
		}
	}
	
	
	int ans=0;
	for(int i=1;i<=n;++i) {
		ans+=abs(indeg[i]-outdeg[i]);
	}
	
	cout<<ans<<"\n";
	for(int i=0;i<m;++i) {
		int ord=index(edgs[i])==edgs[i];
		int van=lst[index(edgs[i])].back();
		
		cout<<(ord!=van?"<-":"->")<<" ";
		
		lst[index(edgs[i])].pop_back();
	}cout<<"\n";
	return 0;	
}

SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1756 KiB
2Accepted0/059ms14648 KiB
3Accepted2/21ms2244 KiB
4Accepted2/21ms2252 KiB
5Accepted2/22ms2252 KiB
6Accepted2/21ms2252 KiB
7Accepted2/21ms2268 KiB
8Accepted2/22ms2516 KiB
9Accepted2/22ms2340 KiB
10Accepted2/22ms2572 KiB
11Accepted2/22ms2636 KiB
12Accepted2/22ms2632 KiB
13Accepted3/317ms5776 KiB
14Accepted3/350ms9864 KiB
15Accepted3/330ms11260 KiB
16Accepted3/341ms11764 KiB
17Accepted3/3246ms8624 KiB
18Accepted3/337ms12472 KiB
19Accepted3/339ms13008 KiB
20Accepted3/367ms17504 KiB
21Accepted3/371ms19924 KiB
22Accepted3/376ms21980 KiB