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的更多相关文章

  1. 树的直径新求法、codeforces 690C3 Brain Network (hard)

    树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的 ...

  2. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  3. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  4. CodeForces 690C1 Brain Network (easy) (水题,判断树)

    题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...

  5. codeforces 690C3 C3. Brain Network (hard)(lca)

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  6. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  7. codeforces 690C1 C1. Brain Network (easy)(水题)

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  8. Codeforces 690 C3. Brain Network (hard) LCA

    C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to prev ...

  9. Brain Network (medium)(DFS)

    H - Brain Network (medium) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

随机推荐

  1. Secret and Whisper

    这两个应用都是以匿名方式向朋友分享信息. 36氪的网友评论到: Whisper 的厉害之处在于给年轻人提供了一个释放压力的地方,比如说他去年陪孩子看病,这种经历不愿意放到 Facebook 上,但是他 ...

  2. cygwin如何断点续传

    对于Cygwin,如果想安装的东西比较多的话,推荐先选择“Download without installing”,下载完了再从本地安装. 好了,说关于断点续传.我所知道的是—— 网上有说法:下载失败 ...

  3. Java快速排序 分别以数组0位作为基准 和最后一位作为基准的排序演示

    package util; public class Pub { public static void beforeSort(int[] arr){ System.out.println(" ...

  4. 打败Google的灵童今在何方?

    微软和雅虎宣布在搜索和广告上10年合作,这事儿不知是不是前不久虚惊一场的微软收购雅虎案的好戏重演之序幕. 从表面上看,这次的合作改变不了搜索和广告目前的世界格局,也构不成对Google的致命威胁,反倒 ...

  5. 修改MDI工程主框架窗口标题(修改CREATESTRUCT结构体)

    版权声明:本文为博主原创文章,未经博主允许不得转载. //在CMainFrame类的PreCreateWindow函数中加入 m_strTitle = _T("Hello"); c ...

  6. Windows下Java File对象创建文件夹时的一个"坑"

    import java.io.File; import java.io.IOException; public class DirCreate { public static void main(St ...

  7. 对Memcached使用的总结和使用场景

    1.memcached是什么 Memcached 常被用来加速应用程序的处理,在这里,我们将着重于介绍将它部署于应用程序和环境中的最佳实践.这包括应该存储或不应存储哪些.如何处理数据的灵活分布以 及如 ...

  8. How to learn linux device driver

    To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...

  9. apk反编译(8)如何完全防止反编译?

    在android 的应用很难做到完全防止反编译,即使用ProGuard混淆的后,也能得到smali代码,这个语法也很简单,很容易理解. 只能通过增加破解难度和成本,使破解者失去耐心. 其中一个常见解决 ...

  10. JSTL、EL、ONGL、Struts标签的区别与使用

     一.JSTL 来源 我们使用JSP开发信息展现非常方便,也可嵌入java代码用来实现相关逻辑,但同样带来了很多问题: jsp维护难度增加 出事提示不明确,不容易提示 分工不明确等 解决上面的问题可以 ...