답안 #1111101

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111101 2024-11-11T13:44:58 Z huantran Cijanobakterije (COCI21_cijanobakterije) C++17
0 / 70
2 ms 744 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
const int maxn = 2e3 + 5;
const int oo = 1e9 + 7;
const ll inf = 1e18;

int n, m;
int dis[maxn], ans, vs[maxn];
int vertex;
vector<int> adj[maxn];

void bfs(int u) {
    for (int i = 1; i <= n; i++)
        dis[i] = oo;
    
    dis[u] = 0;
    queue<int> q;
    q.push(u);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        vs[v] = 1;

        for (auto j : adj[v]) {
            if (dis[j] > dis[v] + 1) {
                dis[j] = dis[v] + 1;
                ans = max(ans, dis[j]);     
                vertex = j;
                q.push(j);
            }
        }
    }
}

int solve(int u) {
    ans = 0;
    bfs(u);
    bfs(vertex);
    return ans + 1;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("TASK.inp", "r", stdin);
        freopen("TASK.out", "w", stdout);
    #endif

    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }   

    int res = 0;

    for (int i = 1; i <= n; i++) {
        if (!vs[i])
            res += solve(i);
    }

    cout << res;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("TASK.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen("TASK.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -