P1099 트리의 핵

1378 단어
제목:https://www.luogu.org/problem/P1099
                           .
Code:
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=100005;
int n,s,head[N],dep[N],fa[N],cnt,ans=0x3f3f3f3f;
bool vis[N];
struct Node{
    int u,v,w,nxt;
}edge[N*2];
void add(int u,int v,int w){
    ++cnt;
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].nxt=head[u];
    head[u]=cnt;
}
int bfs(int s){
    queue q;
    int depth=s;
    q.push(s);
    fa[s]=0;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=edge[i].nxt){
            int v=edge[i].v;
            if(v!=fa[u]&&!dep[v]){
                dep[v]=dep[u]+edge[i].w;
                fa[v]=u;
                q.push(v);
                depth=dep[depth]s){
            j=fa[j];
        }
        ans=min(ans,max(dep[root]-dep[j],dep[i]));
    }
    for(int i=root;i;i=fa[i]){
        vis[i]=1;
    }
    for(int i=root;i;i=fa[i]){
        ans=max(ans,dfs(i));
    }
    printf("%d
",ans); return 0; }

좋은 웹페이지 즐겨찾기