This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "PRAKTICNI"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 1e5 + 1;
const int logC = 30;
struct DSU{
int root[N], rnk[N];
void init(int n){
for(int i = 1; i <= n; i++){
root[i] = i;
rnk[i] = 0;
}
}
int getRoot(int u){
if (u == root[u])
return u;
root[u] = getRoot(root[u]);
return root[u];
}
bool unite(int u, int v){
u = getRoot(u);
v = getRoot(v);
if (u == v)
return 0;
if (rnk[u] < rnk[v])
swap(u, v);
root[v] = u;
rnk[u] += (rnk[u] == rnk[v]);
return 1;
}
};
struct edge{
int u, v, c;
edge(int _u = 0, int _v = 0, int _c = 0){
u = _u;
v = _v;
c = _c;
}
};
int n, m;
edge e[N];
bool mark[N];
vector <ii> adj[N];
int dist[N];
int bit[logC];
vector <int> res[logC];
void readInput(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++)
scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].c);
}
void dfs(int u, int p){
for(ii e : adj[u]){
int v = e.X, c = e.Y;
if (v == p)
continue;
dist[v] = dist[u] ^ c;
dfs(v, u);
}
}
void buildForest(){
DSU dsu;
dsu.init(n);
for(int i = 1; i <= m; i++){
int u = e[i].u, v = e[i].v, c = e[i].c;
if (dsu.unite(u, v)){
adj[u].push_back(mp(v, c));
adj[v].push_back(mp(u, c));
mark[i] = 1;
}
}
for(int i = 1; i <= n; i++)
if (i == dsu.getRoot(i))
dfs(i, -1);
}
int getDist(int u, int v){
return dist[u] ^ dist[v];
}
void solve(){
vector <int> ind;
for(int i = 1; i <= m; i++)
if (!mark[i]){
e[i].c ^= getDist(e[i].u, e[i].v);
ind.push_back(i);
}
int cnt = 0;
for(int i = 0; i < logC; i++)
bit[i] = -1;
for(int id : ind){
int val = e[id].c;
for(int i = 0; i < logC; i++){
if (!((val >> i) & 1))
continue;
if (bit[i] == -1){
bit[i] = val;
cnt++;
}
val ^= bit[i];
res[i].push_back(id);
}
}
printf("%d\n", cnt);
for(int i = 0; i < logC; i++){
if (bit[i] == -1)
continue;
printf("%d %d ", bit[i], (int) res[i].size());
for(int id : res[i])
printf("%d ", id);
printf("\n");
}
}
int main(){
//freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
readInput();
buildForest();
solve();
return 0;
}
Compilation message (stderr)
parkticni.cpp: In function 'void readInput()':
parkticni.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
parkticni.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |