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. POJ 3270 Cow Sorting(置换群)

    题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...

  2. httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数

    一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...

  3. NET权限系统开源项目

    http://www.cnblogs.com/yubaolee/p/OpenAuth.html http://www.cnblogs.com/guozili/p/3496265.html Sereni ...

  4. HtmlAgilityPack 总结(一)

    一个解析html的C#类库HtmlAgilityPack, HtmlAgilityPack是一个基于.Net的.第三方免费开源的微型类库,主要用于在服务器端解析html文档(在B/S结构的程序中客户端 ...

  5. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题

    maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...

  6. 223. Rectangle Area

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  7. OpenCV源码阅读(1)---matx.h---mat类与vec类

    matx.h matx类是opencv中的一个基础类,其位于core模块中,所执行的操作时opencv矩阵和向量的运算.如果熟悉基于matlab的图像处理,那么很容易想到,所有对图像的操作归根结底都是 ...

  8. Jquery的.post说解

    Jquery的.post说解(一) 准备工作 ·Customer类   public class Customer {     public int Unid { get; set; }     pu ...

  9. 解决 “无法安装 Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持”

    http://blog.csdn.net/davidhsing/article/details/8762621 无法安装Microsoft visual studio 2010 service pac ...

  10. SQLite多线程读写实践及常见问题总结

    多线程读写 SQLite实质上是将数据写入一个文件,通常情况下,在应用的包名下面都能找到xxx.db的文件,拥有root权限的手机,可以通过adb shell,看到data/data/packagen ...