-
Notifications
You must be signed in to change notification settings - Fork 8
/
topological_sort_application.cpp
161 lines (156 loc) · 3.63 KB
/
topological_sort_application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Create MST
* Then for all edges not in MST, start from leaf node of MST and set all edges out from it
* Finally set directions of MST edges starting from leafs so that all in degrees are even property
* is satisfied and finally if root has odd degree output -1 else answer is possible
* https://www.codechef.com/DEC18A/problems/EDGEDIR
*/
#include<bits/stdc++.h>
#define mkp(a,b) make_pair(a,b)
#define eb emplace_back
#define mod (long long)(1e9+7)
using namespace std;
typedef long long l;
typedef unsigned long ul;
typedef pair<l,l> pll;
typedef pair<int,int> pii;
typedef long double ld;
class data
{
public:
int val;
int d;
int pi;
data()= default;
data(int val,int d,int pi)
{
this->val=val;
this->d=d;
this->pi=pi;
}
};
class compare
{
public:
bool operator() (data a,data b)
{
return a.d>b.d;
}
};
void traverse(int root,int pi[],vector<int> nbs[],int visited[],int ins[],vector<int> mst[],set<pii> &directions)
{
for(int i=0;i<mst[root].size();++i)
{
traverse(mst[root][i],pi,nbs,visited,ins,mst,directions);
}
visited[root]=1;
for(int i=0;i<nbs[root].size();++i)
{
if(!visited[nbs[root][i]] && pi[root]!=nbs[root][i])
{
directions.insert(mkp(root,nbs[root][i]));
ins[nbs[root][i]]++;
}
}
}
void traverse2(int root,vector<int> mst[],int ins[],int pi[],set<pii> &directions)
{
for(int i=0;i<mst[root].size();++i)
{
traverse2(mst[root][i],mst,ins,pi,directions);
}
// cout<<"At "<<root<<endl;
if(ins[root]%2==0)
{
if(root!=1)
{
directions.insert(mkp(root,pi[root]));
ins[pi[root]]++;
}
}
else
{
if(root!=1)
{
directions.insert(mkp(pi[root],root));
ins[root]++;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
vector<int> nbs[n+1],mst[n+1];
vector<pii> edges;
for(int i=0;i<m;++i)
{
int u,v;
cin>>u>>v;
nbs[u].eb(v);
nbs[v].eb(u);
edges.eb(u,v);
}
int visited[n+1]={0};
int pi[n+1]={0};
priority_queue<data,vector<data>,compare> pq;
pq.push(data(1,0,-1));
int d[n+1];
for(int i=0;i<=n;++i)
{
d[i]=INT_MAX;
}
while(!pq.empty())
{
data curr=pq.top();
pq.pop();
if(visited[curr.val])
{
continue;
}
visited[curr.val]=1;
if(curr.pi!=-1)
{
pi[curr.val]=curr.pi;
mst[curr.pi].eb(curr.val);
}
for(int i=0;i<nbs[curr.val].size();++i)
{
if(!visited[nbs[curr.val][i]])
{
pq.push(data(nbs[curr.val][i],1,curr.val));
}
}
}
set<pii> directions;
memset(visited,0, sizeof(visited));
int ins[n+1]={0};
traverse(1,pi,nbs,visited,ins,mst,directions);
traverse2(1,mst,ins,pi,directions);
if(ins[1]%2==1)
{
cout<<"-1\n";
continue;
}
for(auto i:edges)
{
// cout<<i.first<<" "<<i.second<<endl;
if(directions.find(i)!=directions.end())
{
cout<<"0 ";
}
else
{
cout<<"1 ";
}
}
cout<<endl;
}
return 0;
}