【bzoj1791】 island 基环树+单调队列

xx

树的直径

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 struct re{
 5   ll v;
 6   ll nex; 
 7   ll w;
 8 };
 9 const ll N=1e6;
10 re edge[N*2];
11 ll f[N],dist[N],tot,n;
12 ll maxer,maxv;
13 bool p[N];
14 struct c1{
15   ll a;
16   ll b;
17   ll w;
18 };
19 c1 cun[N];
20 void ad(ll x,ll y,ll d){
21   tot++;
22   edge[tot].nex=f[x];
23   edge[tot].v=y;
24   edge[tot].w=d;
25   f[x]=tot;
26   cun[tot].a=y;
27   cun[tot].b=x;
28   cun[tot].w=d;
29 }
30 void ad1(ll x,ll y,ll d){
31   tot++;
32   edge[tot].nex=f[x];
33   edge[tot].v=y;
34   edge[tot].w=d;
35   f[x]=tot;
36   }
37 ll loop,cnt[N],lp[N];
38 void dfs1(ll x){
39     cnt[x]++;
40     if(cnt[x]==2){loop=x;return;}
41     if(f[x]==-1)return;
42     dfs1(edge[f[x]].v);
43 }
44 void dfs2(ll x){
45     if(x==loop&&cnt[x]==5)return;
46     cnt[x]=5;
47     tot++;
48     lp[tot]=x;
49     dfs2(edge[f[x]].v);
50 }
51 /*void spfa(ll x){
52   maxer=0;
53   dist[x]=0;
54 //  memset(p,0,sizeof(p));//关注多组数据的读入清空
55   p[x]=0;
56   queue<ll>s;
57   s.push(x);
58   while(s.empty!=1){
59   ll az=s.front();
60   ll k=f[az];
61   s.pop();
62   while(k!=-1){
63   ll dot;
64   dot=edge[k].v;
65   if((dist[dot]<dist[az]+edge[k].w&&dist[dot]!=-1)||dist[dot]==-1){
66    dist[dot]=dist[az]+edge[k].w;
67    if(!p[dot]){p[dot]=1;s.push(dot);}
68    if(dist[dot]>maxer){maxer=dist[dot];maxv=dot;}
69        }
70   k=edge[k].nex;
71      }
72    p[az]=0;
73 }
74 }*/
75 int main(){
76     memset(f,-1,sizeof(-1));
77    cin>>n;
78    for(int i=1;i<=n;i++){
79   ll it1,it2,it3;
80   cin>>it1>>it2>>it3;
81   ad(it1,it2,it3);
82 }
83    tot=n;
84    for(int i=1;i<=n;i++)ad1(cun[i].a,cun[i].b,cun[i].w);
85    return 0; 
86 }
View Code

 

 http://www.cnblogs.com/Khada-Jhin/p/9468144.html

https://blog.csdn.net/lvzelong2014/article/details/80647506?utm_source=blogxgwz4

posted @ 2018-10-16 18:11  周栎  阅读(202)  评论(0编辑  收藏  举报