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+5;
const LL mod=1e9+7;
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=1;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 0;}
    }
    if(n==1)printf("yes\n");
    else printf("no\n");
    return 0;
}
View Code

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+5;
const LL mod=1e9+7;
int head[N],tot,n,m,d[N>>1],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,-1,sizeof(d));
   queue<int>q;q.push(u);
   d[u]=0;
   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]!=-1)continue;
         d[v]=d[x]+1;
         q.push(v);
         if(d[v]>d[ret])ret=v;
     }
   }
}
int main(){
   scanf("%d%d",&n,&m);
   memset(head,-1,sizeof(head));tot=0;
   for(int i=1;i<n;++i){
     int u,v;
     scanf("%d%d",&u,&v);
     add(u,v);add(v,u);
   }
   ret=1;
   bfs(1);bfs(ret);
   printf("%d\n",d[ret]);
   return 0;
}
View Code

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+5;
const LL mod=1e9+7;
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][20];
void dfs(int u,int f){
   fa[u][0]=f;d[u]=d[f]+1;
   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=0;t;t>>=1,++i)
    if(t&1)u=fa[u][i];
   if(u==v)return u;
   for(int i=19;i>=0;--i){
     if(fa[u][i]!=-1&&fa[u][i]!=fa[v][i])
      u=fa[u][i],v=fa[v][i];
   }
   return fa[u][0];
}
int main(){
   scanf("%d",&n);
   memset(head,-1,sizeof(head));
   for(int i=2;i<=n;++i){
     int u;scanf("%d",&u);add(u,i);
   }
   memset(fa,-1,sizeof(fa));
   dfs(1,0);fa[1][0]=-1;
   for(int j=1;(1<<j)<=n;++j)
    for(int i=1;i<=n;++i)
      if(fa[i][j-1]!=-1)
        fa[i][j]=fa[fa[i][j-1]][j-1];
   printf("1");
   int x=1,y=2,z=1;
   for(int i=3;i<=n;++i){
      int tpx=LCA(x,i),lenx=d[x]+d[i]-2*d[tpx];
      int tpy=LCA(y,i),leny=d[y]+d[i]-2*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 0;
}
View Code

 

posted @ 2016-07-12 19:09  shuguangzw  阅读(228)  评论(0编辑  收藏  举报