#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pii pair<int,int>
#define pis pair<int,short>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const int oo = 2e9;
const int N = 100100;
const int PW = 30;
vector<int> extra, vec[PW];
vector<pii> g[N], vc;
int U[N], V[N], C[N], pre[N], xr[N], tt = 0;
int XOR = -1, n, m, basis[PW], siz = 0, id[PW], obr[PW];
bool cmp(int _x, int _y){
return C[_x] < C[_y];
}
int get(int x) { return (pre[x] == x ? x : pre[x] = get(pre[x])); }
void dfs(int v, int p){
for (pii u : g[v]){
if (u.ft == p) continue;
xr[u.ft] = (xr[v] ^ u.sd);
dfs(u.ft, v);
}
}
void insert_to_basis(int x){
for (int po = 0; po < PW; po++){
if (!(x & (1 << po)))
continue;
if (basis[po] == 0) {
basis[po] = x;
id[po] = siz;
obr[siz] = po;
siz++;
return;
} else x ^= basis[po];
}
}
void add(pii x){
for (int po = 0; po < PW; po++){
if (!(x.sd & (1 << po)))
continue;
assert(basis[po] != 0);
vec[id[po]].PB(x.ft);
x.sd ^= basis[po];
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < n; i++)
pre[i] = i;
for (int i = 0; i < m; i++){
cin >> U[i] >> V[i] >> C[i];
U[i]--; V[i]--;
}
for (int i = m - 1; i >= 0; i--){
int x = get(U[i]), y = get(V[i]);
if (x == y) {
extra.PB(i);
} else {
pre[x] = y;
g[U[i]].PB(MP(V[i], C[i]));
g[V[i]].PB(MP(U[i], C[i]));
}
}
dfs(0, 0);
for (int nm : extra){
int x = U[nm], y = V[nm];
int cxr = (xr[x] ^ xr[y] ^ C[nm]);
if (cxr != 0){
vc.PB(MP(nm, cxr));
insert_to_basis(cxr);
}
}
if (!sz(vc))
cout << 0;
else {
cout << siz << '\n';
for (pii cr : vc)
add(cr);
for (int i = 0; i < siz; i++){
cout << basis[obr[i]] << " " << sz(vec[i]);
for (int cr : vec[i])
cout << " " << cr + 1;
cout << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
5752 KB |
Output is correct |
2 |
Correct |
37 ms |
6264 KB |
Output is correct |
3 |
Correct |
11 ms |
3328 KB |
Output is correct |
4 |
Correct |
12 ms |
3456 KB |
Output is correct |
5 |
Correct |
77 ms |
11768 KB |
Output is correct |
6 |
Correct |
86 ms |
11384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3968 KB |
Output is correct |
2 |
Correct |
20 ms |
4096 KB |
Output is correct |
3 |
Correct |
24 ms |
4736 KB |
Output is correct |
4 |
Correct |
26 ms |
5052 KB |
Output is correct |
5 |
Correct |
66 ms |
8568 KB |
Output is correct |
6 |
Correct |
38 ms |
6392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
7032 KB |
Output is correct |
2 |
Correct |
58 ms |
8696 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
9336 KB |
Output is correct |
2 |
Correct |
106 ms |
13040 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
7672 KB |
Output is correct |
2 |
Correct |
66 ms |
9208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
9724 KB |
Output is correct |
2 |
Correct |
41 ms |
6904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
10236 KB |
Output is correct |
2 |
Correct |
98 ms |
11904 KB |
Output is correct |
3 |
Correct |
61 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
13048 KB |
Output is correct |
2 |
Correct |
145 ms |
14720 KB |
Output is correct |
3 |
Correct |
119 ms |
14192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
5240 KB |
Output is correct |
2 |
Correct |
34 ms |
5880 KB |
Output is correct |
3 |
Correct |
84 ms |
10616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
11128 KB |
Output is correct |
2 |
Correct |
55 ms |
7672 KB |
Output is correct |
3 |
Correct |
109 ms |
13168 KB |
Output is correct |