#include
using namespace std;
const int maxn=2e5+5;
const double eps=1e-5;
int n,ans;
typedef pair<double,double> pdd;
map<pdd,int>mp;
pdd p[maxn];
pdd circle_center(double x1,double y1,double x2,double y2,double x3,double y3)//三点共圆模板
{
double a=x1-x2;
double b=y1-y2;
double c=x1-x3;
double d=y1-y3;
double e=((x1*x1-x2*x2)+(y1*y1-y2*y2))/2.0;
double f=((x1*x1-x3*x3)+(y1*y1-y3*y3))/2.0;
double det=b*c-a*d;
if(det<eps&&det>(-eps))//三点共线
return pdd{0,0};
double x=-(d*e-b*f)/det;
double y=-(a*f-c*e)/det;
return pdd{x,y};//满足条件返回圆心
}
int main()
{
scanf("%d",&n);
//p[0]=make_pair(0,0);
for(int i=1;i<=n;i++)
{
double x,y;
scanf("%lf %lf",&x,&y);
p[i]=make_pair(x,y);
}
for(int i=1;i<=n;i++)
{
mp.clear();
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
double x1=p[i].first,y1=p[i].second,x2=p[j].first,y2=p[j].second,x3=0,y3=0;
pdd tmp=circle_center(x1,y1,x2,y2,x3,x3);
if(tmp!=pdd{0,0})
ans=max(ans,++mp[tmp]);
}
}
printf("%d\n",ans+1);
return 0;
}
第三,n>2且叶子节点数为奇数,只要在根添一个儿子节点,那么叶子节点数就变成偶数了,而且把根当作抽象的新的儿子效果是一样的,因此我们添一个儿子节点为根节点。
AC代码
#include
using namespace std;
const int maxn=2e5+5;
struct node
{
int to,next;
}edge[maxn<<1];
int head[maxn];
int ing[maxn];
vector<int>ans;
int cnt,root,n;
void add(int x,int y)
{
edge[++cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt;
}
void dfs(int u,int fa)
{
if(ing[u]==1)
{
ans.push_back(u);
return ;
}
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa)
continue;
dfs(v,u);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
ing[u]++;
ing[v]++;
add(u,v);
add(v,u);
}
if(n==1)
{
puts("0");
return 0;
}
if(n==2)
{
puts("1");
puts("1 2");
return 0;
}
for(int i=1;i<=n;i++)
if(ing[i]>1)
{
root=i;
break;
}
dfs(root,0);
if(ans.size()%2==1)
ans.push_back(root);
printf("%d\n",ans.size()>>1);
for(int i=1;i<=(ans.size()>>1);i++)
{
printf("%d %d\n",ans[i-1],ans[i+(ans.size()>>1)-1]);
}
return 0;
}