-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.cpp
More file actions
153 lines (125 loc) · 3.14 KB
/
a.cpp
File metadata and controls
153 lines (125 loc) · 3.14 KB
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
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fr(i, n) for(int i = 0; i < n; i++)
#define frr(i, n) for(int i = 1; i <= n; i++)
#define dbg(x) cout << #x << " = " << x << endl
#define all(x) x.begin(),x.end()
#define ms(x, i) memset(x, i, sizeof(x))
#define p(x) cout << x << "\n"
#define pv(x) fr(i, x.size()) cout << x[i] << " \n"[i==x.size()-1]
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f;
#define maxn 212345
vector<pii> adj[maxn];
pii edg[maxn];
int flag[maxn], vis[maxn], edt[maxn];
int cnta, cntb, ndeu;
void dfs(int u, bool c) {
if (flag[u] != -1 && c != flag[u]) ndeu = 1;
if (vis[u]) return;
vis[u] = 1;
flag[u] = c;
if (c) cnta++;
else cntb++;
for (auto x: adj[u]) {
if (x.s != 1) continue;
dfs(x.f, !c);
}
}
int main() {
fastio;
ms(flag, -1);
int n, m;
cin >> n >> m;
fr(i, m) {
int u, v, c;
cin >> u >> v >> c;
adj[u].pb({v,c});
adj[v].pb({u,c});
edg[i] = {u,v};
edt[i] = c;
}
queue<int> q;
fr(i, m) {
if (edt[i] != 2) continue;
int u = edg[i].f, v = edg[i].s;
flag[u] = flag[v] = 1;
q.push(u);
q.push(v);
}
fr(i, m) {
if (edt[i] != 0) continue;
int u = edg[i].f, v = edg[i].s;
if (flag[u] == 1 || flag[v] == 1) {
p("impossible");
return 0;
}
flag[u] = flag[v] = 0;
q.push(u);
q.push(v);
}
while (!q.empty()) {
int u = q.front();
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto x: adj[u]) {
int v = x.f;
int c = x.s;
if (c == 0) {
if (flag[v] == 1) {
p("impossible");
return 0;
}
flag[v] = 0;
q.push(v);
}
else if (c == 1) {
if (flag[u] == 1) {
if (flag[v] == 1) {
p("impossible"); return 0;
}
flag[v] = 0;
q.push(v);
}
else {
if (flag[v] == 0) {
p("impossible"); return 0;
}
flag[v] = 1;
q.push(v);
}
}
else if (c == 2) {
if (flag[v] == 0) {
p("impossible"); return 0;
}
flag[v] = 1;
q.push(v);
}
}
}
int ans = 0;
frr(i, n) ans += (flag[i] == 1);
frr(i, n) {
if (vis[i]) continue;
cnta = cntb = 0;
dfs(i, 0);
ans += min(cnta, cntb);
if (ndeu) {
p("impossible");
return 0;
}
}
p(ans);
}