Submission #638842

# Submission time Handle Problem Language Result Execution time Memory
638842 2022-09-07T16:27:19 Z NotLinux Cijanobakterije (COCI21_cijanobakterije) C++14
70 / 70
65 ms 15436 KB
/**
 * author: NotLinux
 * created: 07.09.2022 ~ 18:41:58
**/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL
    #include "/home/notlinux/debug.h"
#else
    #define debug(x...) void(37)
#endif
vector < vector < int > > graph;
vector < int > par;
vector < int > vis;
vector < int > visdsu;
pair < int , int >  dfs(int node , int past){
    pair < int , int > ret = {node,1};
    for(auto itr : graph[node]){
        if(itr == past)continue;
        pair < int , int > dumb = dfs(itr,node);
        debug(itr,node,dumb);
        if(ret.second < (dumb.second+1)){
            ret = {dumb.first , dumb.second+1};
        }
    }
    return {ret.first , ret.second};
}
int longest_path(int st){
    debug(st);
    int node1 = dfs(st,0).first;
    debug(node1);
    int rams = dfs(node1,0).second;
    debug(st,rams);
    return rams;
}
int find(int a){
    if(par[a] == a)return a;    
    par[a] = find(par[a]);
    return par[a];
}
void merge(int a, int b){
    par[find(a)] = find(b);
}
void solve(){
    int n,m;cin >> n >> m;

    par.resize(n+1);
    iota(par.begin(),par.end(),0LL);
    graph.resize(n+1);
    vis.resize(n+1);
    visdsu.resize(n+1);

    while(m--){
        int a,b;cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
        merge(a,b);
    }
    int ans = 0;
    for(int i = 1;i<=n;i++){
        if(visdsu[find(i)] == 0){
            ans += longest_path(i);
            visdsu[find(i)] = 1;
        }
    }
    cout << ans << endl;
}
int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);
    int tt=1;
    //cin >> tt;
    while(tt--)solve();
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1876 KB Output is correct
2 Correct 14 ms 3464 KB Output is correct
3 Correct 25 ms 5068 KB Output is correct
4 Correct 33 ms 6620 KB Output is correct
5 Correct 42 ms 8304 KB Output is correct
6 Correct 65 ms 9808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 15436 KB Output is correct
2 Correct 3 ms 1492 KB Output is correct
3 Correct 6 ms 2772 KB Output is correct
4 Correct 11 ms 4048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 4 ms 1868 KB Output is correct
6 Correct 7 ms 3412 KB Output is correct
7 Correct 10 ms 4836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1876 KB Output is correct
2 Correct 14 ms 3464 KB Output is correct
3 Correct 25 ms 5068 KB Output is correct
4 Correct 33 ms 6620 KB Output is correct
5 Correct 42 ms 8304 KB Output is correct
6 Correct 65 ms 9808 KB Output is correct
7 Correct 30 ms 15436 KB Output is correct
8 Correct 3 ms 1492 KB Output is correct
9 Correct 6 ms 2772 KB Output is correct
10 Correct 11 ms 4048 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 4 ms 1868 KB Output is correct
16 Correct 7 ms 3412 KB Output is correct
17 Correct 10 ms 4836 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 57 ms 9896 KB Output is correct
30 Correct 10 ms 6076 KB Output is correct
31 Correct 29 ms 8860 KB Output is correct
32 Correct 17 ms 6868 KB Output is correct
33 Correct 33 ms 9492 KB Output is correct
34 Correct 24 ms 7644 KB Output is correct
35 Correct 32 ms 9080 KB Output is correct
36 Correct 34 ms 8276 KB Output is correct
37 Correct 34 ms 9420 KB Output is correct
38 Correct 41 ms 9032 KB Output is correct
39 Correct 32 ms 9156 KB Output is correct