답안 #850136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850136 2023-09-15T19:48:39 Z HoriaHaivas Bread First Search (CCO21_day2problem2) C++14
0 / 25
2 ms 5960 KB
/*
    "TLE is like the wind, always by my side"
    - Yasuo - 2022 -
*/
#include<bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")

using namespace std;

struct VOLUNTARI
{
    int node;
    int dist;
};

const int inf=200001;
vector<int> nodes[200001];
int distans[200001];
bool visited[200001];
int n;
queue<VOLUNTARI> q;

void bfs(int source)
{
    int j;
    for (j=1;j<=n;j++)
    {
         distans[j]=inf;
         visited[j]=0;
    }
    q.push({source,0});
    VOLUNTARI fr;
    while (!q.empty())
    {
        fr=q.front();
        if (!visited[fr.node])
        {
            visited[fr.node]=1;
            distans[fr.node]=fr.dist;
            for (j=0;j<nodes[fr.node].size();j++)
            {
                 q.push({nodes[fr.node][j],fr.dist+1});
            }
        }
        q.pop();
    }
}

bool sorteaza (int prefixlen)
{
    int j;
    for (j=2;j<=prefixlen;j++)
    {
         nodes[1].push_back(j);
         nodes[j].push_back(1);
    }
    bfs(1);
    for (j=prefixlen;j>=2;j--)
    {
         nodes[1].pop_back();
         nodes[j].pop_back();
    }
    bool ok;
    ok=true;
    for (j=2;j<=n;j++)
    {
         if (distans[j]<distans[j-1] || !visited[j])
             ok=false;
    }
    return ok;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int m,i,j,k,r,pas,u,v,ans;
    cin >> n >> m;
    for (j=1;j<=m;j++)
    {
         cin >> u >> v;
         nodes[u].push_back(v);
         nodes[v].push_back(u);
    }
    r=0;
    pas=(1<<17);
    while(pas)
    {
        if (r+pas<=n && !sorteaza(r+pas))
            r+=pas;
        pas=pas/2;
    }
    r++;
    bfs(1);
    ans=0;
    for (j=1;j<=r;j++)
    {
         if (distans[j]==1 || j==1)
             r--;
    }
    cout << r;
    return 0;
}

Compilation message

Main.cpp: In function 'void bfs(int)':
Main.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |             for (j=0;j<nodes[fr.node].size();j++)
      |                      ~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:80:11: warning: unused variable 'i' [-Wunused-variable]
   80 |     int m,i,j,k,r,pas,u,v,ans;
      |           ^
Main.cpp:80:15: warning: unused variable 'k' [-Wunused-variable]
   80 |     int m,i,j,k,r,pas,u,v,ans;
      |               ^
Main.cpp:80:27: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
   80 |     int m,i,j,k,r,pas,u,v,ans;
      |                           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 1 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Incorrect 2 ms 5960 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 1 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Incorrect 2 ms 5960 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 1 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Incorrect 2 ms 5960 KB Output isn't correct
5 Halted 0 ms 0 KB -