As usual, Alice finishes her delicious cake at noon. Unfortunately, the smell of cake beckoned hungry Bob, and he decided to rob one piece of cake.
The cake is a convex polygon with N edges. At the beginning, Bob cut it along the diagonals. After M cuts, Bob decided to rob the 'largest' piece of cake. Strangely, in Bob's opinion, the piece has the most number of edge is the biggest one.
Please help Bob to find the 'largest' piece.
Input
There are multiple test cases (about 20).
The first line of each test case contains two integer number N, M (5 <= N <= 10000), indicating the number of point of the cake and the cut, respectively.
The following M lines contain two integer x, y (1 <= x, y <= N), denoting the index of the starting and ending cut point. (the index of points mark from 1 to N clockwise.)
The input will guarantee that all of the cuts will not intersect inside the cake, but they may cross each other at the edge of cake, and Bob won't cut along the initial edge of the cake.
Output
Output the maximal size (most number of edges) of the piece which Bob will get.
Sample Input
7 2 3 6 7 2
Sample Output
4
题意:给出一个n多边形,然后在上面按给定的切m刀,问边数最多的一块是哪一块。相当于是求最小环中,路径最长是多少。
思路:暴力枚举每一块,一个点被共用的次数等于该点被切到的次数+1。为避免重复枚举,只需从1到n依次枚举过来,每次枚举的时候把当前点与下一个点放在同一个块中,从下一个点开始依次寻找最内侧的点,直到形成环,边数就等于环内的结点数
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
#define SIZE 10005
vector<int> node[SIZE];
int flag[SIZE],ans,n;
void slove(int u){
flag[u]++;
int v=(u+1>n?1:u+1),next;
flag[v]++;
int res=2;
while(1){
next=(v+1>n?1:v+1);
for(int i=0;i<node[v].size();i++)
if(next>u&&((node[v][i]>next)||(node[v][i]<=u)))
next=node[v][i];
else if(next<=u&&node[v][i]<=u&&node[v][i]>next)
next=node[v][i];
if(next==u){
if(res>ans)
ans=res;
return;
}
else {
v=next;
flag[v]++;
res++;
}
}
}
int main(void){
int m;
int s,e;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++)
node[i].clear(),flag[i]=0;
ans=0;
while(m--){
scanf("%d%d",&s,&e);
node[s].push_back(e);
node[e].push_back(s);
}
for(int i=1;i<=n;i++)
if(flag[i]<node[i].size()+1)
slove(i);
printf("%d\n",ans);
}
return 0;
}