[APIO 2010] 巡逻

2021-03-26 17:26

阅读:620

标签:pair   div   push   namespace   tin   define   dfs   first   continue   

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=1912

[算法]

         树的直径

[代码]

        

#includeusing namespace std;
#define MAXN 100010

int i,n,l1,l2,tot,u,v,k,now;
int head[MAXN],pre[MAXN],d[MAXN];
vectorint > path;
pairint,pairint,int> > tmp;

struct edge
{
        int to,w,nxt;
} e[MAXN 1];

inline void addedge(int u,int v,int w)
{
        tot++;
        e[tot] = (edge){v,w,head[u]};
        head[u] = tot;
}
inline void dfs1(int u,int fa)
{
        int i,v,w;
        for (i = head[u]; i; i = e[i].nxt)
        {
                v = e[i].to;
                w = e[i].w;
                if (v == fa) continue;
                dfs1(v,u);
                if (d[u] + d[v] + w  > l1)
                {
                        l1 = d[u] + d[v] + w;
                        tmp = make_pair(i,make_pair(pre[u],pre[v]));
                }
                if (d[v] + w > d[u]) 
                {
                        d[u] = d[v] + w;
                        pre[u] = i;
                }
        }
}
inline void dfs2(int u,int fa)
{
        int i,v,w;
        for (i = head[u]; i; i = e[i].nxt)
        {
                v = e[i].to;
                w = e[i].w;
                if (v == fa) continue;
                dfs2(v,u);
                if (d[u] + d[v] + w > l2) l2 = d[u] + d[v] + w;
                d[u] = max(d[u],d[v] + w); 
        } 
}
int main() 
{
        
        scanf("%d%d",&n,&k);
        for (i = 1; i )
        {
                scanf("%d%d",&u,&v);
                addedge(u,v,1);
                addedge(v,u,1);
        }
        l1 = 0;
        memset(d,0,sizeof(d));
        dfs1(1,0);
        now = tmp.second.first;
        while (now != 0)
        {
                path.push_back(now);
                now = pre[e[now].to];
        }
        path.push_back(tmp.first);
        now = tmp.second.second;
        while (now != 0)
        {
                path.push_back(now);
                now = pre[e[now].to];
        }
        for (i = 0; i ) 
        {
                e[path[i]].w *= -1;
                e[path[i] ^ 1].w *= -1;
        }
        l2 = 0;
        memset(d,0,sizeof(d));
        dfs2(1,0);
        if (k == 1) printf("%d\n",2 * (n - 1) - l1 + 1); 
        else printf("%d\n",2 * n - l1 - l2);
        
        return 0;
    
}

 

[APIO 2010] 巡逻

标签:pair   div   push   namespace   tin   define   dfs   first   continue   

原文地址:https://www.cnblogs.com/evenbao/p/9379106.html


评论


亲,登录后才可以留言!