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 ...
随机推荐
- HttpWebRequest和HttpWebResponse
原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...
- DVB系统几种传输方式
卫星 (DVB-S 及 DVB-S2)有线 (DVB-C)地面无线 (DVB-T)手持地面无线 (DVB-H)
- MySQL 当记录不存在时插入(insert if not exists)
在 MySQL 中,插入(insert)一条记录很简单,但是一些特殊应用,在插入记录前,需要检查这条记录是否已经存在,只有当记录不存在时才执行插入操作,本文介绍的就是这个问题的解决方案.问题:我创建了 ...
- Rockethon 2015
A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...
- Android中使用HTTP和HttpClient进行通信
/** * 使用HTTP的Get方式进行数据请求 */ protected void httpGet() { /** * 进行异步请求 */ new AsyncTask<String, Void ...
- SpingMVC中利用BindingResult将错误信息返回到页面中
SpingMVC中利用BindingResult将错误信息返回到页面中. ActionFrom中: private String name; private String password; get( ...
- login.java
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Login extends JFrame ...
- 72. Edit Distance
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- Centos硬件信息查看命令
[root@yan-001 ~] # uname -a # 查看内核/操作系统/CPU信息的linux系统信息命令 [root@yan-001 ~] # head -n 1 /etc/issue # ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...