답안 #55662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
55662 2018-07-08T12:01:51 Z red1108 문명 (KOI17_civilization) C++17
100 / 100
600 ms 30136 KB
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
#define x first
#define y second
P t;
queue<P> q;
int table[2010][2010],ans=0,cnt,n,k;
int parent[100010];
int par(int a)
{
    if(parent[a]==a) return a;
    return parent[a]=par(parent[a]);
}
void uni(int a, int b)
{
    if(par(a)!=par(b))
    {
        parent[par(a)]=par(b);
        cnt--;
    }
}
bool ch(int a, int b)
{
    if(a<1||b<1||a>n||b>n) return 0;
    return 1;
}
void view(int a, int b)
{
        if(table[a-1][b]) uni(table[a-1][b],table[a][b]);
        if(table[a+1][b]) uni(table[a+1][b],table[a][b]);
        if(table[a][b+1]) uni(table[a][b+1],table[a][b]);
        if(table[a][b-1]) uni(table[a][b-1],table[a][b]);
}
int main()
{
    int i, j,a,b,si;
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(i=1;i<=k;i++)
    {
        cin>>a>>b;
        table[a][b]=i;
        parent[i]=i;
        cnt++;
        if(table[a-1][b]) uni(table[a-1][b],table[a][b]);
        if(table[a+1][b]) uni(table[a+1][b],table[a][b]);
        if(table[a][b+1]) uni(table[a][b+1],table[a][b]);
        if(table[a][b-1]) uni(table[a][b-1],table[a][b]);
        q.push({a,b});
    }
    if(cnt==1)
    {
        cout<<0;
        return 0;
    }
    while(!q.empty())
    {
        ans++;
        si=q.size();
        for(i=1;i<=si;i++) {
            a=q.front().x;
            b=q.front().y;
            q.pop();
            if(table[a+1][b]==0&&ch(a+1,b)){table[a+1][b]=par(table[a][b]);q.push({a+1,b});view(a+1,b);}
            if(table[a-1][b]==0&&ch(a-1,b)){table[a-1][b]=par(table[a][b]);q.push({a-1,b});view(a-1,b);}
            if(table[a][b+1]==0&&ch(a,b+1)){table[a][b+1]=par(table[a][b]);q.push({a,b+1});view(a,b+1);}
            if(table[a][b-1]==0&&ch(a,b-1)){table[a][b-1]=par(table[a][b]);q.push({a,b-1});view(a,b-1);}
        }
        if(cnt==1)
        {
            cout<<ans;
            return 0;
        }
    }
}

Compilation message

civilization.cpp: In function 'int main()':
civilization.cpp:39:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j,a,b,si;
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 868 KB Output is correct
3 Correct 4 ms 868 KB Output is correct
4 Correct 3 ms 872 KB Output is correct
5 Correct 4 ms 928 KB Output is correct
6 Correct 3 ms 976 KB Output is correct
7 Correct 3 ms 976 KB Output is correct
8 Correct 3 ms 992 KB Output is correct
9 Correct 3 ms 1028 KB Output is correct
10 Correct 3 ms 1028 KB Output is correct
11 Correct 4 ms 1028 KB Output is correct
12 Correct 4 ms 1032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 868 KB Output is correct
3 Correct 4 ms 868 KB Output is correct
4 Correct 3 ms 872 KB Output is correct
5 Correct 4 ms 928 KB Output is correct
6 Correct 3 ms 976 KB Output is correct
7 Correct 3 ms 976 KB Output is correct
8 Correct 3 ms 992 KB Output is correct
9 Correct 3 ms 1028 KB Output is correct
10 Correct 3 ms 1028 KB Output is correct
11 Correct 4 ms 1028 KB Output is correct
12 Correct 50 ms 8328 KB Output is correct
13 Correct 37 ms 8328 KB Output is correct
14 Correct 59 ms 8328 KB Output is correct
15 Correct 32 ms 8328 KB Output is correct
16 Correct 12 ms 8328 KB Output is correct
17 Correct 169 ms 12616 KB Output is correct
18 Correct 172 ms 13348 KB Output is correct
19 Correct 147 ms 14112 KB Output is correct
20 Correct 170 ms 15036 KB Output is correct
21 Correct 144 ms 15656 KB Output is correct
22 Correct 3 ms 15656 KB Output is correct
23 Correct 45 ms 15656 KB Output is correct
24 Correct 4 ms 1032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 760 KB Output is correct
2 Correct 3 ms 868 KB Output is correct
3 Correct 4 ms 868 KB Output is correct
4 Correct 3 ms 872 KB Output is correct
5 Correct 4 ms 928 KB Output is correct
6 Correct 3 ms 976 KB Output is correct
7 Correct 3 ms 976 KB Output is correct
8 Correct 3 ms 992 KB Output is correct
9 Correct 3 ms 1028 KB Output is correct
10 Correct 3 ms 1028 KB Output is correct
11 Correct 4 ms 1028 KB Output is correct
12 Correct 50 ms 8328 KB Output is correct
13 Correct 37 ms 8328 KB Output is correct
14 Correct 59 ms 8328 KB Output is correct
15 Correct 32 ms 8328 KB Output is correct
16 Correct 12 ms 8328 KB Output is correct
17 Correct 169 ms 12616 KB Output is correct
18 Correct 172 ms 13348 KB Output is correct
19 Correct 147 ms 14112 KB Output is correct
20 Correct 170 ms 15036 KB Output is correct
21 Correct 144 ms 15656 KB Output is correct
22 Correct 3 ms 15656 KB Output is correct
23 Correct 45 ms 15656 KB Output is correct
24 Correct 137 ms 19784 KB Output is correct
25 Correct 181 ms 20344 KB Output is correct
26 Correct 105 ms 20344 KB Output is correct
27 Correct 155 ms 20344 KB Output is correct
28 Correct 85 ms 20344 KB Output is correct
29 Correct 563 ms 25436 KB Output is correct
30 Correct 526 ms 25436 KB Output is correct
31 Correct 581 ms 28400 KB Output is correct
32 Correct 570 ms 29312 KB Output is correct
33 Correct 600 ms 30136 KB Output is correct
34 Correct 136 ms 30136 KB Output is correct
35 Correct 3 ms 30136 KB Output is correct
36 Correct 4 ms 1032 KB Output is correct