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;
}

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;
}

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;
}

时间: 2024-10-19 08:12:24

codeforces 690C3 Brain Network的相关文章

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

题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> G[maxn]; int f[maxn], g[maxn], l[maxn]; int dfs(int root, int fa){ if(f[root] !=

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

题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn =1000 + 5; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){ int n, m, x, y; cin

Codeforces 690 C3. Brain Network (hard) LCA

C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consum

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

CodeForces 707A Brain&#39;s Photos (水题)

题意:给一张照片的像素,让你来确定是黑白的还是彩色的. 析:很简单么,如果有一种颜色不是黑白灰,那么就一定是彩色的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #i

Codeforces 707A. Brain&#39;s Photos

题目链接:http://codeforces.com/problemset/problem/707/A 题意: 给你一个 n * m 的照片, 让你判断这个照片是否是黑白的,其中如果一个照片仅由"W", "B", "G"组成, 那么就可以说它是黑白的. 代码: 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 typedef long long LL; 6 const do

【模拟】Codeforces 707A Brain&#39;s Photos

题目链接: http://codeforces.com/problemset/problem/707/A 题目大意: 给一张N*M的图,只有六种颜色(如下),只含B,W,G的是黑白图,否则是彩色图.问这张图是什么图. 'C' (cyan) 'M' (magenta) 'Y' (yellow) 'W' (white) 'G' (grey) 'B' (black) 题目思路: [模拟] 签到题.直接mark记一下是否出现彩色即可. 1 // 2 //by coolxxx 3 //#include<b

Brain Network (easy)

One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (

complex brain network

Organization, development and function of complex brain networks The Brain as a Complex System: Using Network Science as a Tool for Understanding the Brain Rubinov M, Sporns O. Complex network measures of brain connectivity: uses and interpretations.