codeforces 690C3 Brain Network
simple:并查集一下
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+;
const LL mod=1e9+;
int fa[N];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)fa[i]=i;
while(m--){
int u,v;
scanf("%d%d",&u,&v);
u=find(u),v=find(v);
if(u!=v)fa[u]=v,--n;
else{printf("no\n");return ;}
}
if(n==)printf("yes\n");
else printf("no\n");
return ;
}
medium:最长路
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+;
const LL mod=1e9+;
int head[N],tot,n,m,d[N>>],ret;
struct Edge{
int v,next;
}edge[N];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void bfs(int u){
memset(d,-,sizeof(d));
queue<int>q;q.push(u);
d[u]=;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];~i;i=edge[i].next){
int v=edge[i].v;
if(d[v]!=-)continue;
d[v]=d[x]+;
q.push(v);
if(d[v]>d[ret])ret=v;
}
}
}
int main(){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));tot=;
for(int i=;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
ret=;
bfs();bfs(ret);
printf("%d\n",d[ret]);
return ;
}
hard:动态最长路,LCA维护
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+;
const LL mod=1e9+;
int head[N],tot,n,d[N];
struct Edge{
int v,next;
}edge[N];
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int fa[N][];
void dfs(int u,int f){
fa[u][]=f;d[u]=d[f]+;
for(int i=head[u];~i;i=edge[i].next)dfs(edge[i].v,u);
}
int LCA(int u,int v){
if(d[u]<d[v])swap(u,v);
for(int t=d[u]-d[v],i=;t;t>>=,++i)
if(t&)u=fa[u][i];
if(u==v)return u;
for(int i=;i>=;--i){
if(fa[u][i]!=-&&fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
}
return fa[u][];
}
int main(){
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;++i){
int u;scanf("%d",&u);add(u,i);
}
memset(fa,-,sizeof(fa));
dfs(,);fa[][]=-;
for(int j=;(<<j)<=n;++j)
for(int i=;i<=n;++i)
if(fa[i][j-]!=-)
fa[i][j]=fa[fa[i][j-]][j-];
printf("");
int x=,y=,z=;
for(int i=;i<=n;++i){
int tpx=LCA(x,i),lenx=d[x]+d[i]-*d[tpx];
int tpy=LCA(y,i),leny=d[y]+d[i]-*d[tpy];
if(lenx>=leny&&lenx>=z){
y=i;z=lenx;
}
else if(leny>=lenx&&leny>=z){
x=i;z=leny;
}
printf(" %d",z);
}
printf("\n");
return ;
}
codeforces 690C3 Brain Network的更多相关文章
- 树的直径新求法、codeforces 690C3 Brain Network (hard)
树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的 ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- CodeForces 690C1 Brain Network (easy) (水题,判断树)
题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
随机推荐
- POJ2632Crashing Robots
做模拟题做的我直接睡着了,题并不难,就是一个细心的问题,有一些细节问题注意了就差不多了,代码写的精美的一般找错误也好找一些,应该学着些好看的代码 #include<cstdio> #inc ...
- Project Euler 84:Monopoly odds 大富翁几率
Monopoly odds In the game, Monopoly, the standard board is set up in the following way: ...
- lintcode:在二叉查找树中插入节点
题目: 在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树. 样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样 ...
- Ajax省市联动
以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- MyEclipse +Tomcat 异常操作
安装完MyEclipse 2014,用JAX-WS的方式写了一个小段Web Service,用MyEclipse 自带的 Tomcat 部署没有问题,用我自己的,却出现下面的错误: java.lang ...
- underscore.js 一个强大的js函数库
Underscore提供的100多个函数,主要涉及对Collection.Object.Array.Function的操作: Collections(集合) each, map, reduce, re ...
- Hibernate开发之二 映射主键-
<class name="cn.itcast.e_hbm_id.User" table="user"> <!-- 映射主 ...
- arcgis engine 开发教程系列
版权声明: <ArcGIS Engine+C#实例开发教程>为3SDN(http://www.3sdn.net)原创教程,版权所有.禁止商业用途转载(如需请联系作者),非商业 ...
- oracle视图总结(转)
视图简介: 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表.视图是存储在数据字典里的一条select语句. 通过创建视图可以提取数 ...
- launch genymotion simulator from command line
Command to launch genymotion headless - player --vm-name Nexus_4 if player is not already added to p ...