답안 #412671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412671 2021-05-27T10:21:59 Z 반딧불(#7608) Bread First Search (CCO21_day2problem2) C++17
0 / 25
4 ms 4940 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, m;
vector<int> link[200002];
int dist[200002];
int ans;

multiset<int> st;

int main(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y);
        link[y].push_back(x);
    }

    for(int i=2; i<=n; i++) dist[i] = 1e9;
    queue<int> que;
    que.push(1);
    while(!que.empty()){
        int x = que.front();
        que.pop();

        for(auto y: link[x]){
            if(dist[y] == 1e9){
                dist[y] = dist[x] + 1;
                que.push(y);
            }
        }
    }

    for(int i=2; i<=n; i++){
        st.insert(dist[i]);
    }
    st.insert(1e9);
    for(int i=2; i<=n; i++){
        st.erase(st.find(dist[i]));
        if(dist[i] == 1e9 || dist[i] > *st.begin()){
            ans++;
            dist[i] = (i==2 ? 1 : dist[i-1]);
            for(auto y: link[i]){
                if(dist[y] > dist[i]+1){
                    st.erase(st.find(dist[y]));
                    dist[y] = dist[i]+1;
                    st.insert(dist[y]);
                }
            }
        }
    }
    printf("%d", ans);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Incorrect 3 ms 4940 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Incorrect 3 ms 4940 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Incorrect 3 ms 4940 KB Output isn't correct
6 Halted 0 ms 0 KB -