标签:没有 while tmp each i++ cst ref efi typedef
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4812
题目:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4660 Accepted Submission(s): 930
1 #include <cstdio>
2 #include <algorithm>
3 #include <vector>
4 #include <cmath>
5 #include <cstring>
6 using namespace std;
7
8 #define MP make_pair
9 #define PB push_back
10 typedef long long LL;
11 typedef pair<int,int> PII;
12 const double eps=1e-8;
13 const double pi=acos(-1.0);
14 const int K=1e6+7;
15 const int mod=1e6+3;
16
17 PII ans,mx;
18 vector<int >mp[K];
19 int cnt,tk,vis[K],dis[K],tdis[K];
20 int fd[K],v[K],hs[K];
21 int qpow(int x,int y)
22 {
23 int ret=1;
24 for(;y;y>>=1,x=((LL)x*x)%mod)
25 if(y&1) ret=((LL)ret*x)%mod;
26 return ret;
27 }
28 void pre()
29 {
30 for(int i=1;i<mod;i++)
31 fd[i]=qpow(i,mod-2);
32 }
33 struct CenterTree
34 {
35 int n,ret,mx,son[K];
36 void dfs(int x,int f)
37 {
38 son[x]=1;
39 int tmp=0;
40 for(int i=0;i<mp[x].size();i++)
41 if(f!=mp[x][i] && !vis[mp[x][i]])
42 {
43 dfs(mp[x][i],x);
44 son[x]+=son[mp[x][i]];
45 tmp=max(tmp,son[mp[x][i]]);
46 }
47 tmp=max(tmp,n-son[x]);
48 if(tmp<mx)
49 mx=tmp,ret=x;
50 }
51 int getCenter(int x,int num)
52 {
53 n=num,mx=0x3f3f3f3f;
54 dfs(x,0);
55 return ret;
56 }
57 }ct;
58 void sc(int x,int f,LL td)
59 {
60 dis[++cnt]=td,tdis[cnt]=x;
61 for(int i=0;i<mp[x].size();i++)
62 if(!vis[mp[x][i]] && f!=mp[x][i])
63 sc(mp[x][i],x,(td*v[mp[x][i]])%mod);
64 }
65 void calc(int x)
66 {
67 hs[v[x]]=x;
68 for(int i=0;i<mp[x].size();i++)
69 if(!vis[mp[x][i]])
70 {
71 cnt=0;
72 sc(mp[x][i],x,v[mp[x][i]]);
73 for(int j=1;j<=cnt;j++)
74 {
75 int tx=fd[((LL)dis[j]*fd[tk])%mod];
76 if(hs[tx])
77 ans=min(ans,MP(min(hs[tx],tdis[j]),max(hs[tx],tdis[j])));
78 }
79 for(int j=1;j<=cnt;j++)
80 {
81 int tx=((LL)dis[j]*v[x])%mod;
82 if(!hs[tx]||(hs[tx]!=0&&hs[tx]>tdis[j]))
83 hs[tx]=tdis[j];
84 }
85
86 }
87 hs[v[x]]=0;
88 for(int i=0;i<mp[x].size();i++)
89 if(!vis[mp[x][i]])
90 {
91 cnt=0;
92 sc(mp[x][i],x,v[mp[x][i]]);
93 for(int j=1;j<=cnt;j++)
94 hs[((LL)dis[j]*v[x])%mod]=0;
95 }
96 }
97 void solve(int x)
98 {
99 vis[x]=1;
100 calc(x);
101 for(int i=0;i<mp[x].size();i++)
102 if(!vis[mp[x][i]])
103 solve(ct.getCenter(mp[x][i],ct.son[mp[x][i]]));
104 }
105 int main(void)
106 {
107 int n;
108 pre();
109 //freopen("in.acm","r",stdin);
110 mx=MP(0x3f3f3f3f,0x3f3f3f3f);
111 while(~scanf("%d%d",&n,&tk))
112 {
113 for(int i=1;i<=n;i++)
114 scanf("%d",v+i),vis[i]=0,mp[i].clear();
115 for(int i=1,x,y;i<n;i++)
116 scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
117 ans=mx;
118 solve(ct.getCenter(1,n));
119 if(ans!=mx)
120 printf("%d %d\n",ans.first,ans.second);
121 else
122 printf("No solution\n");
123 }
124 return 0;
125 }
标签:没有 while tmp each i++ cst ref efi typedef
原文地址:http://www.cnblogs.com/weeping/p/6905480.html