cf734 E. Anton and Tree
这个题的题意还是很劲的。搞了好久才知道是怎么变得。
(假设已经缩好了点,每次边中间颜色不同的,然后和就和他外面的相同,继续再变这个大的,依次类推,最多就是树的直径D/2)
(还是英语水平太弱了(吐槽++,数学考得还没英语高,还是找个地方撞死吧2333))
#include<bits/stdc++.h>
#define lowbit(x) x&(-x)
#define LL long long
#define N 200005
#define M 1000005
#define mod 1000000007LL
#define inf 0x7ffffffff
using namespace std;
inline int ra()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>''){if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
struct node{
int next,to;
}re[N<<],e[N<<];
int head[N],hd[N],cnt,recnt;
void insert(int x, int y)
{
e[++cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt;
}
void reinsert(int x, int y)
{
re[++recnt].to=y;
re[recnt].next=hd[x];
hd[x]=recnt;
}
int ans,sum[N],belong[N],mx[N],sec[N],n,deep[N];
bool col[N];
void dfs(int x, int num)
{
belong[x]=num;
for (int i=head[x];i;i=e[i].next)
{
if (belong[e[i].to]) continue;
if (col[e[i].to]==col[x]) dfs(e[i].to,num);
}
}
void rebuild()
{
for (int i=; i<=n; i++)
{
for (int j=head[i];j;j=e[j].next)
{
if (belong[e[j].to]==belong[i]) continue;
reinsert(belong[i],belong[e[j].to]);
}
}
}
void dfs1(int x, int fa)
{
deep[x]=; int mx=,sc=;
for (int i=hd[x];i;i=re[i].next)
{
if (re[i].to==fa) continue;
dfs1(re[i].to,x);
deep[x]=max(deep[x],deep[re[i].to]+);
if (mx==deep[re[i].to]) sc=mx;
if (mx<deep[re[i].to]) sc=mx,mx=deep[re[i].to];
if (mx>deep[re[i].to]) sc=max(sc,deep[re[i].to]);
}
ans=max(ans,max(deep[x],mx+sc+));
}
int main()
{
n=ra();
for (int i=; i<=n; i++) col[i]=ra();
for (int i=; i<n; i++)
{
int x=ra(),y=ra();
insert(x,y);
insert(y,x);
}
int tot=;
for (int i=; i<=n; i++)
if (!belong[i]) dfs(i,++tot);
rebuild();
dfs1(,);
cout<<ans/;
return ;
} //一开始没看懂题意2333,以为好水
/*#include<bits/stdc++.h>
#define lowbit(x) x&(-x)
#define LL long long
#define N 200005
#define M 1000005
#define mod 1000000007LL
#define inf 0x7ffffffff
using namespace std;
inline int ra()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9'){if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct node{
int next,to;
}e[N<<1];
int head[N],cnt;
bool vis[N],col[N];
inline void insert(int x, int y)
{
e[++cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt;
}
void dfs(int x, int fa)
{
vis[x]=1;
for (int i=head[x];i;i=e[i].next)
{
if (e[i].to==fa || col[e[i].to]!=col[x] || vis[e[i].to]) continue;
dfs(e[i].to,x);
}
}
int n,ans1,ans0;
int main()
{
n=ra();
for (int i=1; i<=n; i++) col[i]=ra();
for (int i=1; i<n; i++)
{
int x=ra(),y=ra();
insert(x,y);
insert(y,x);
}
for (int i=1; i<=n; i++)
if (col[i]==0 && !vis[i]) ++ans0,dfs(i,0);
memset(vis,0,sizeof(vis));
for (int i=1; i<=n; i++)
if (col[i]==1 && !vis[i]) ++ans1,dfs(i,0);
cout<<min(ans1,ans0);
return 0;
}*/
cf734 E. Anton and Tree的更多相关文章
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- Anton and Tree
Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- Codeforces734 E. Anton and Tree
传送门:>Here< 题意:给出一颗树,节点不是黑色就是白色,每次可以将一个颜色相同的块变颜色,问最少变几次才能让其变为同色 解题思路: 我们考虑由于每一次都是把同样颜色的色块进行变色,因 ...
随机推荐
- 设置span 宽度的完美解决方案
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mac home/end/pageup/pageDown
home:按fn+左键 end:按fn+右键 pageup:按fn+上键 pagedown:按fn+下键
- DML、DDL、DCL区别及介绍
DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作 ...
- php 安装php5-mysql 拓展
Your PHP installation appears to be missing the MySQL extension which is required by WordPress Error ...
- Redis 代理 twemproxy
4台 redis 服务器 172.16.1.37:6379 - 1 172.16.1.36:6379 - 2 172.16.1.35:6379 - 3 172.16.1.34:6379 ...
- AWK----awk与shell交互
1互相调用命令 ls | awk '{if(system("ls " $0)==0) print "file "$0" exsits"}' ...
- linux日常巡检脚本
######################以下是脚本内容开始部分###################################### #!/bin/bash #set -x2012-02-2 ...
- GCJ Round 1C 2009 Problem C. Bribe the Prisoners
区间DP.dp[i][j]表示第i到第j个全部释放最小费用. #include<cstdio> #include<cstring> #include<cmath> ...
- 百度api集合!
百度 api集市免费接口 IP地址查询 http://apistore.baidu.com/apiworks/servicedetail/114.html 频道新闻API_易源 http://apis ...
- vmware克隆Centos6.7虚拟机网卡无法启动问题
快速处理办法: cat /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i '/UUID/d' /etc/sysconfig/network-script ...