# BZOJ3697: 采药人的路径

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000000+5
14 #define maxm 20000000+5
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
23 #define mod 1000000007
24 using namespace std;
26 {
27     int x=0,f=1;char ch=getchar();
28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
30     return x*f;
31 }
32 struct edge{int go,next,w;}e[maxn];
34 ll ans;
35 bool del[maxn];
36 inline void insert(int x,int y,int z)
37 {
40 }
41 inline void getrt(int x,int fa)
42 {
43     s[x]=1;f[x]=0;
44     for4(i,x)if(!del[y=e[i].go]&&y!=fa)
45     {
46         getrt(y,x);
47         s[x]+=s[y];
48         f[x]=max(f[x],s[y]);
49     }
50     f[x]=max(f[x],sum-f[x]);
51     if(f[x]<f[rt])rt=x;
52 }
53 inline void get(int x,int fa,int w)
54 {
55     d[++cnt2][0]=w;d[cnt2][1]=v[w+n];
56     v[w+n]++;
57     for4(i,x)if(!del[y=e[i].go]&&y!=fa)get(y,x,w+e[i].w);
58     v[w+n]--;
59 }
60 inline void work(int x)
61 {
62     del[x]=1;cnt1=0;cnt2=0;
63     for4(i,x)if(!del[y=e[i].go])
64     {
65         get(y,x,e[i].w);
66         for2(j,cnt1+1,cnt2)ans+=(ll)((d[j][1]!=0)*g[-d[j][0]+n][0]+g[-d[j][0]+n][1]);
67         for2(j,cnt1+1,cnt2)g[d[j][0]+n][d[j][1]!=0]++;
68         cnt1=cnt2;
69     }
70     for1(j,cnt2){if(d[j][0]==0&&d[j][1]>1)ans++;g[d[j][0]+n][0]=g[d[j][0]+n][1]=0;}
71     for4(i,x)if(!del[y=e[i].go])
72     {
73         sum=s[y];rt=0;
74         getrt(y,0);
75         work(rt);
76     }
77 }
78 int main()
79 {
80     freopen("input.txt","r",stdin);
81     freopen("output.txt","w",stdout);
84     sum=n;f[rt=0]=inf;v[n]=1;
85     getrt(1,0);
86     work(rt);
87     cout<<ans<<endl;
88     return 0;
89
90 }  
View Code

## 3697: 采药人的路径

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 71  Solved: 27
[Submit][Status]

7
l 2 0
3 1 1
2 4 0
5 2 0
6 3 1
5 7 1

1

## HINT

posted @ 2014-12-20 10:18  ZYF-ZYF  Views(103)  Comments(0Edit  收藏