#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
ll n,k,p[4000004],transp[2004][2004],cnt,coj,anw,vis[4000004];
bool ck[4000004];
ll dx[]={1,-1,0,0},dy[]={0,0,1,-1};
vector<pi> ar;
queue<pi> q,wq;
ll par(int a)
{
if(p[a]==a)
return a;
return p[a]=par(p[a]);
}
bool merge(ll a,ll b) //a's par to b
{
a=par(a),b=par(b);
if(a!=b)
{
p[a]=b;
return 1;
}
return 0;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(NULL);
cin>>n>>k;
for(int i=0;i<k;i++)
{
ll a,b;cin>>a>>b;
ar.push_back({a,b});
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
transp[i][j]=++cnt;
for(int i=1;i<=n*n+1;i++)
p[i]=i;
for(int i=0;i<k;i++)
{
wq.push(ar[i]);
ck[transp[ar[i].f][ar[i].s]]=1;
}
while(1)
{
bool flag=1;
anw++;
while(!wq.empty())
{
pi a=wq.front();
q.push(a);
wq.pop();
}
while(!q.empty())
{
ll nwx=q.front().f,nwy=q.front().s;
q.pop();
for(int i=0;i<4;i++)
{
ll ntx=nwx+dx[i],nty=nwy+dy[i];
if(ntx<1||ntx>n||nty<1||nty>n)
continue;
if(ck[p[transp[ntx][nty]]])
{
if(merge(transp[ntx][nty],transp[nwx][nwy]))
{
if(vis[transp[ntx][nty]]==anw)
flag=0;
coj++;
}
}
else
{
vis[transp[ntx][nty]]=anw;
if(merge(transp[ntx][nty],transp[nwx][nwy]))
wq.push({ntx,nty});
}
}
}
if(coj==k-1)
{
if(flag)
cout<<anw-1;
else
cout<<anw;
break;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
4 ms |
992 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
4 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
4 ms |
992 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
150 ms |
27940 KB |
Output is correct |
13 |
Correct |
114 ms |
27896 KB |
Output is correct |
14 |
Correct |
140 ms |
27868 KB |
Output is correct |
15 |
Correct |
100 ms |
27700 KB |
Output is correct |
16 |
Correct |
28 ms |
22484 KB |
Output is correct |
17 |
Incorrect |
607 ms |
36980 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
4 ms |
992 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
150 ms |
27940 KB |
Output is correct |
13 |
Correct |
114 ms |
27896 KB |
Output is correct |
14 |
Correct |
140 ms |
27868 KB |
Output is correct |
15 |
Correct |
100 ms |
27700 KB |
Output is correct |
16 |
Correct |
28 ms |
22484 KB |
Output is correct |
17 |
Incorrect |
607 ms |
36980 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |