| Task: | Connect cities |
| Sender: | Isak |
| Submission time: | 2025-09-08 16:09:20 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.10 s | details |
| #7 | ACCEPTED | 0.10 s | details |
| #8 | ACCEPTED | 0.10 s | details |
| #9 | ACCEPTED | 0.10 s | details |
| #10 | ACCEPTED | 0.10 s | details |
| #11 | ACCEPTED | 0.03 s | details |
| #12 | ACCEPTED | 0.00 s | details |
Code
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/queue.h>
typedef struct node{
int city;
SLIST_ENTRY(node) entry;
} node;
SLIST_HEAD(list, node);
typedef struct super_node{
list l;
SLIST_ENTRY(super_node) entry;
} super_node;
SLIST_HEAD(super_list, super_node);
void rec_construct_node(uint32_t n, list *l, list *graph, bool* alreday_seen){
if (alreday_seen[n]){
return;
}
alreday_seen[n] = true;
node *el = (node*) malloc(sizeof(node));
el->city = n;
SLIST_INSERT_HEAD(l, el, entry);
node *cur;
SLIST_FOREACH(cur, &graph[n], entry){
rec_construct_node(cur->city, l, graph, alreday_seen);
}
}
super_node *construct_node(uint32_t n, uint32_t nb_node, list *graph, bool* alreday_seen){
super_node *sn = (super_node*) malloc(sizeof(super_node));
SLIST_INIT(&(sn->l));
rec_construct_node(n, &(sn->l), graph, alreday_seen);
return sn;
}
int main() {
uint32_t n = 0;
uint32_t m = 1;
if (scanf("%d %d", &n, &m) == 0)
return EXIT_FAILURE;
list *links = (list *) malloc(n * sizeof(list));
for (uint32_t i = 0; i < n; i++){
SLIST_INIT(links + i);
}
// graph construction
uint32_t x, y;
node *tmp;
for(uint32_t i = 0; i < m; i++){
if (scanf("%d %d", &x, &y) == 0)
return EXIT_FAILURE;
x--; y--;
tmp = (node*) malloc(sizeof(node));
tmp->city = y;
SLIST_INSERT_HEAD(links + x, tmp,entry);
tmp = (node*) malloc(sizeof(node));
tmp->city = x;
SLIST_INSERT_HEAD(links + y, tmp,entry);
}
bool *cheked = (bool*) calloc(n, sizeof(bool));
super_list sl;
SLIST_INIT(&sl);
for(uint32_t i = 0; i < n; i++){
if (!cheked[i]){
super_node *sn = construct_node(i, n, links, cheked);
SLIST_INSERT_HEAD(&sl, sn, entry);
}
}
uint32_t size = 0;
super_node *cur;
SLIST_FOREACH(cur, &sl, entry) size++;
if (size > 1){
printf("%d\n", size - 1);
uint32_t the_chosen = SLIST_FIRST(&SLIST_FIRST(&sl)->l)->city;
SLIST_REMOVE_HEAD(&sl, entry);
SLIST_FOREACH(cur, &sl, entry) printf("%d %d\n", the_chosen + 1, SLIST_FIRST(&cur->l)->city + 1);
} else {
printf("%d\n", size-1);
}
return EXIT_SUCCESS;
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 10 2 5 5 6 1 4 6 8 ... |
| correct output |
|---|
| 2 1 2 2 7 |
| user output |
|---|
| 2 7 9 7 4 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 10 3 9 6 8 9 10 7 8 ... |
| correct output |
|---|
| 2 1 4 4 5 |
| user output |
|---|
| 2 5 4 5 3 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 10 7 9 1 7 1 3 3 4 ... |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 10 4 8 5 9 4 9 2 7 ... |
| correct output |
|---|
| 1 1 3 |
| user output |
|---|
| 1 3 6 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 10 4 9 2 4 7 10 1 8 ... |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 7233 22146 94937 96203 6133 10731 98737 99193 ... |
| correct output |
|---|
| 4785 1 2 2 3 3 4 4 5 ... |
| user output |
|---|
| 4785 92713 90963 92713 90909 92713 89009 92713 88246 ... Truncated |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 92950 93575 24401 88897 41796 99364 47106 50330 ... |
| correct output |
|---|
| 4868 1 2 2 7 7 9 9 15 ... |
| user output |
|---|
| 4868 97442 96502 97442 95399 97442 94571 97442 92574 ... Truncated |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 15637 76736 79169 98809 4382 86557 73383 77029 ... |
| correct output |
|---|
| 4683 1 9 9 20 20 27 27 28 ... |
| user output |
|---|
| 4683 97820 96774 97820 94574 97820 93462 97820 91778 ... Truncated |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 47932 66981 86401 99942 4353 27841 60492 67345 ... |
| correct output |
|---|
| 4807 1 6 6 7 7 11 11 12 ... |
| user output |
|---|
| 4807 93550 92311 93550 90640 93550 90394 93550 88574 ... Truncated |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 6554 44548 76413 98555 5447 59589 70166 74434 ... |
| correct output |
|---|
| 4786 1 2 2 18 18 21 21 27 ... |
| user output |
|---|
| 4786 93323 92125 93323 92024 93323 91577 93323 90805 ... Truncated |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 100000 1 1 2 |
| correct output |
|---|
| 99998 1 3 3 4 4 5 5 6 ... |
| user output |
|---|
| 99998 100000 99999 100000 99998 100000 99997 100000 99996 ... Truncated |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 10 9 2 5 5 6 1 4 6 8 ... |
| correct output |
|---|
| 2 1 2 2 7 |
| user output |
|---|
| 2 7 5 7 4 |
