Submission #948869

# Submission time Handle Problem Language Result Execution time Memory
948869 2024-03-18T15:42:59 Z vin Cijanobakterije (COCI21_cijanobakterije) C++17
15 / 70
26 ms 11100 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>

const int INF = 2147483645;
const int maxN = (int)1e5+5;
const ll LLINF = LLONG_MAX;
//const ll mod = 998244353;
const ll mod = 1000000007;

vector<int> adj[maxN];
int dist[maxN];
bool vis[maxN], bck[maxN];

void dfs(int s) {
	for (auto i: adj[s]) {
		if (vis[i]) continue;
		vis[i] = true;
		dist[i] = dist[s]+1;
		dfs(i);
	}
}

void solv() {
	int n, m, a, b;
    cin>>n>>m;
    for (int i=0;i<m;i++) {
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1);
    int mx = 0, start = 1;
    for (int i=1;i<=n;i++) {
        if (dist[i] > mx) {
            mx = dist[i];
            start = i;
        }
    }
    memset(dist, 0, sizeof dist);
    memset(vis, 0, sizeof vis);
    dfs(start);
    mx = 0;
    for (int i=1;i<=n;i++) {
        mx = max(mx, dist[i]);
    }
    if (vis[2]) {
    	cout<<mx+1<<'\n';
    	return;
	}
    memset(dist, 0, sizeof dist);
    memset(vis, 0, sizeof vis);
    dfs(2);
    int mx2 = 0;
    start = 2;
    for (int i=1;i<=n;i++) {
        if (dist[i] > mx2) {
            mx2 = dist[i];
            start = i;
        }
    }
    memset(dist, 0, sizeof dist);
    memset(vis, 0, sizeof vis);
    dfs(start);
    mx2 = 0;
    for (int i=1;i<=n;i++) {
        mx2 = max(mx2, dist[i]);
    }
    cout<<mx+mx2+2<<'\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t=1;
//    cin>>t;
    while (t--) solv();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3672 KB Output is correct
2 Correct 9 ms 4188 KB Output is correct
3 Correct 16 ms 4888 KB Output is correct
4 Correct 20 ms 5292 KB Output is correct
5 Correct 26 ms 5908 KB Output is correct
6 Correct 25 ms 6524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 11100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3164 KB Output is correct
2 Incorrect 1 ms 3164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Incorrect 1 ms 3164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3672 KB Output is correct
2 Correct 9 ms 4188 KB Output is correct
3 Correct 16 ms 4888 KB Output is correct
4 Correct 20 ms 5292 KB Output is correct
5 Correct 26 ms 5908 KB Output is correct
6 Correct 25 ms 6524 KB Output is correct
7 Incorrect 23 ms 11100 KB Output isn't correct
8 Halted 0 ms 0 KB -