Submission details
Task:Connect cities
Sender:datxaban
Submission time:2025-09-03 20:38:59 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.12 sdetails
#7ACCEPTED0.12 sdetails
#8ACCEPTED0.12 sdetails
#9ACCEPTED0.12 sdetails
#10ACCEPTED0.12 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.02 sdetails

Compiler report

input/code.cpp: In function 'void solve()':
input/code.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int i = 0; i < ans.size() - 1; i++){
      |                    ~~^~~~~~~~~~~~~~~~
input/code.cpp:79:9: warning: unused variable 'cnt' [-Wunused-variable]
   79 |     int cnt = 0;
      |         ^~~

Code

/*
    Road to Freedom
*/

#include <bits/stdc++.h>

using namespace std;

#define ll             long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
#define REP(i,a,b)     for (int i = a; i <= b; i++)

inline int power(int a, int b)
{
    int x = 1;
    while (b)
    {
        if (b & 1) x *= a;
        a *= a;
        b >>= 1;
    }
    return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr (names + 1, ',');
    cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// const int N = 200005;
const int N = 10e5 + 5;
vector<int> g[N];


void dfs(int i, vector<bool>& visit){
    visit[i] = true;
    for(auto& x: g[i]){
        if(!visit[x]) {
            dfs(x, visit);
        }
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    
    for(int i = 1; i <= m; i++)  {
        int a,b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    vector<bool> visit(n+1, false);

    int cnt = 0;
    vector<int> ans;
    for(int i = 1; i <= n;i++){
        if(!visit[i]) {
            dfs(i, visit);
            ans.push_back(i);
        }
    }
    cout << ans.size() - 1 << endl;
    for(int i = 0; i < ans.size() - 1; i++){
        cout << ans[i] << " " << ans[i+ 1] << endl;
    }

}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #ifndef ONLINE_JUDGE
        freopen("input.txt",  "r",  stdin);
        freopen("output.txt", "w", stdout);
    #endif

    clock_t z = clock();

    int t = 1;
    // cin >> t;
    while (t--) solve();

    cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);

    return 0;
}

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
1 2
2 7

Error:
Run Time : 6.5e-05

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
1 4
4 5

Error:
Run Time : 6.6e-05

Test 3

Verdict: ACCEPTED

input
10 10
7 9
1 7
1 3
3 4
...

correct output
0

user output
0

Error:
Run Time : 6.4e-05

Test 4

Verdict: ACCEPTED

input
10 10
4 8
5 9
4 9
2 7
...

correct output
1
1 3

user output
1
1 3

Error:
Run Time : 6.5e-05

Test 5

Verdict: ACCEPTED

input
10 10
4 9
2 4
7 10
1 8
...

correct output
0

user output
0

Error:
Run Time : 6.2e-05

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
1 2
2 3
3 4
4 5
...
Truncated

Error:
Run Time : 0.094152

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
1 2
2 7
7 9
9 15
...
Truncated

Error:
Run Time : 0.095411

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
1 9
9 20
20 27
27 28
...
Truncated

Error:
Run Time : 0.095337

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
1 6
6 7
7 11
11 12
...
Truncated

Error:
Run Time : 0.095474

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
1 2
2 18
18 21
21 27
...
Truncated

Error:
Run Time : 0.094708

Test 11

Verdict: ACCEPTED

input
100000 1
1 2

correct output
99998
1 3
3 4
4 5
5 6
...

user output
99998
1 3
3 4
4 5
5 6
...
Truncated

Error:
Run Time : 0.020307

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
1 2
2 7

Error:
Run Time : 6.5e-05