Submission #64405442
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <inttypes.h>struct edge_s {int to;int value;};int ec[212345];struct edge_s* es[212345];void ae(int f, int t, int v) {es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));if (es[f] == NULL) exit(2);es[f][ec[f]++] = (struct edge_s){ t, v };}int value[212345];int count[32][2];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
struct edge_s {
int to;
int value;
};
int ec[212345];
struct edge_s* es[212345];
void ae(int f, int t, int v) {
es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
if (es[f] == NULL) exit(2);
es[f][ec[f]++] = (struct edge_s){ t, v };
}
int value[212345];
int count[32][2];
int cnt;
int targets[212345];
int ans[212345];
void dfs(int node, int cur_value) {
if (value[node] >= 0) {
if (value[node] != cur_value) {
puts("-1");
exit(0);
}
} else {
int i;
for (i = 0; i < 31; i++) {
count[i][(cur_value >> i) & 1]++;
}
value[node] = cur_value;
targets[cnt++] = node;
for (i = 0; i < ec[node]; i++) {
dfs(es[node][i].to, cur_value ^ es[node][i].value);
}
}
}
int main(void) {
int N, M;
int i;
if (scanf("%d%d", &N, &M) != 2) return 1;
for (i = 0; i < M; i++) {
int X, Y, Z;
if (scanf("%d%d%d", &X, &Y, &Z) != 3) return 1;
ae(X, Y, Z);
ae(Y, X, Z);
}
memset(value, -1, sizeof(value));
for (i = 1; i <= N; i++) {
if (value[i] < 0) {
int j;
int meow = 0;
memset(count, 0, sizeof(count));
cnt = 0;
dfs(i, 0);
for (j = 0; j < 31; j++) {
if (count[j][0] < count[j][1]) meow |= 1 << j;
}
for (j = 0; j < cnt; j++) {
ans[targets[j]] = value[targets[j]] ^ meow;
}
}
}
for (i = 0; i < N; i++) {
printf(" %d" + !i, ans[i + 1]);
}
putchar('\n');
return 0;
}
/*
各連結成分ごとに、1箇所決めるとほかも全部決まる
各ビットについて 0 or 1 の数をそれぞれ数え、多くない方を採用
矛盾したら失格
*/
Submission Info
| Submission Time | |
|---|---|
| Task | E - Min of Restricted Sum |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 450 |
| Code Size | 1705 Byte |
| Status | AC |
| Exec Time | 60 ms |
| Memory | 12028 KB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 450 / 450 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 1 ms | 2556 KB |
| 00_sample_01.txt | AC | 1 ms | 2336 KB |
| 00_sample_02.txt | AC | 1 ms | 2432 KB |
| 01_handmade_00.txt | AC | 1 ms | 2572 KB |
| 01_handmade_01.txt | AC | 20 ms | 3320 KB |
| 01_handmade_02.txt | AC | 1 ms | 2352 KB |
| 01_handmade_03.txt | AC | 1 ms | 2436 KB |
| 01_handmade_04.txt | AC | 1 ms | 2436 KB |
| 01_handmade_05.txt | AC | 1 ms | 2452 KB |
| 01_handmade_06.txt | AC | 20 ms | 3356 KB |
| 01_handmade_07.txt | AC | 49 ms | 11852 KB |
| 01_handmade_08.txt | AC | 43 ms | 11832 KB |
| 01_handmade_09.txt | AC | 28 ms | 4704 KB |
| 02_random_00.txt | AC | 41 ms | 7732 KB |
| 02_random_01.txt | AC | 42 ms | 8384 KB |
| 02_random_02.txt | AC | 54 ms | 9248 KB |
| 02_random_03.txt | AC | 60 ms | 9644 KB |
| 02_random_04.txt | AC | 25 ms | 6120 KB |
| 02_random_05.txt | AC | 44 ms | 8364 KB |
| 02_random_06.txt | AC | 46 ms | 8536 KB |
| 02_random_07.txt | AC | 60 ms | 9692 KB |
| 02_random_08.txt | AC | 31 ms | 6716 KB |
| 02_random_09.txt | AC | 33 ms | 7432 KB |
| 02_random_10.txt | AC | 31 ms | 4336 KB |
| 02_random_11.txt | AC | 35 ms | 7492 KB |
| 02_random_12.txt | AC | 31 ms | 4760 KB |
| 02_random_13.txt | AC | 35 ms | 9648 KB |
| 02_random_14.txt | AC | 21 ms | 5040 KB |
| 02_random_15.txt | AC | 26 ms | 8724 KB |
| 02_random_16.txt | AC | 32 ms | 5084 KB |
| 02_random_17.txt | AC | 36 ms | 9580 KB |
| 02_random_18.txt | AC | 29 ms | 6592 KB |
| 02_random_19.txt | AC | 58 ms | 9524 KB |
| 02_random_20.txt | AC | 9 ms | 4140 KB |
| 02_random_21.txt | AC | 48 ms | 12028 KB |
| 02_random_22.txt | AC | 27 ms | 5680 KB |
| 02_random_23.txt | AC | 45 ms | 7720 KB |
| 02_random_24.txt | AC | 33 ms | 5316 KB |