HDU 4812 D Tree

来源:互联网 发布:淘宝视频制作软件 编辑:程序博客网 时间:2024/06/06 13:02

Description

There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a vertex). Today the students under the tree are considering a problem: Can we find such a chain on the tree so that the multiplication of all integers on the chain (mod 10 6 + 3) equals to K? 
Can you help them in solving this problem?
 

Input

There are several test cases, please process till EOF. 
Each test case starts with a line containing two integers N(1 <= N <= 10 5) and K(0 <=K < 10 6 + 3). The following line contains n numbers v i(1 <= v i < 10 6 + 3), where vi indicates the integer on vertex i. Then follows N - 1 lines. Each line contains two integers x and y, representing an undirected edge between vertex x and vertex y.
 

Output

For each test case, print a single line containing two integers a and b (where a < b), representing the two endpoints of the chain. If multiply solutions exist, please print the lexicographically smallest one. In case no solution exists, print “No solution”(without quotes) instead. 
For more information, please refer to the Sample Output below.
 

Sample Input

5 602 5 2 3 31 21 32 42 55 22 5 2 3 31 21 32 42 5
 

Sample Output

3 4No solution

Hint

 1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较,若第一个数字大小相同,则按照第二个数字大小进行比较,依次类推。 2. 若出现栈溢出,推荐使用C++语言提交,并通过以下方式扩栈: #pragma comment(linker,"/STACK:102400000,102400000")          
 乘法逆元+树分治+hash,写完一A了,略开心。
#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int INF = 0x7FFFFFFF;const int mod = 1e6 + 3;const int maxn = 2e5 + 10;int n, K, x, y, inv[mod], h[mod], f[mod];struct Tree{int ft[maxn], nt[maxn], u[maxn], v[maxn], sz, n;int vis[maxn], cnt[maxn], mx[maxn], a, b, flag;void clear(int n){this->n = n;mx[0] = INF;a = b = sz = flag = 0;memset(h, 0, sizeof(h));for (int i = 1; i <= n; i++){ft[i] = -1;vis[i] = 0;scanf("%d", &v[i]);}}void AddEdge(int x, int y){u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++;}int dfs(int x, int fa, int sum){int ans = mx[x] = 0;cnt[x] = 1;for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]] || u[i] == fa) continue;int y = dfs(u[i], x, sum);if (mx[y]<mx[ans]) ans = y;cnt[x] += cnt[u[i]];mx[x] = max(mx[x], cnt[u[i]]);}mx[x] = max(mx[x], sum - cnt[x]);return mx[x] < mx[ans] ? x : ans;}void get(int x, int fa, int now, int kind){if (!kind){int y = (LL)K*inv[now] % mod;if (h[y] == flag){if (a + b == 0) a = min(f[y], x), b = max(f[y], x);else{if (a > min(f[y], x)) a = min(f[y], x), b = max(f[y], x);else if (a == min(f[y], x) && b > max(f[y], x)) b = max(f[y], x);}}}else{if (h[now] != flag) h[now] = flag, f[now] = x;else f[now] = min(f[now], x);}for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]] || u[i] == fa) continue;get(u[i], x, (LL)now*v[u[i]] % mod, kind);}}void find(int x){h[1] = ++flag;f[1] = x;for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]]) continue;get(u[i], x, (LL)v[u[i]] * v[x] % mod, 0);get(u[i], x, v[u[i]], 1);}}void work(int x, int sum){int y = dfs(x, -1, sum);vis[y] = 1; find(y);for (int i = ft[y]; i != -1; i = nt[i]){if (vis[u[i]]) continue;if (cnt[u[i]] > cnt[y]) cnt[u[i]] = sum - cnt[y];work(u[i], cnt[u[i]]);}}}solve;void read(int &x){char ch;while ((ch = getchar()) < '0' || ch > '9');x = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';}void init(){inv[0] = 0;inv[1] = 1;for (int i = 2; i < mod; i++) inv[i] = (LL)inv[mod%i] * (mod - mod / i) % mod;}int main(){init();while (~scanf("%d%d", &n, &K)){solve.clear(n);for (int i = 1; i < n; i++){scanf("%d%d", &x, &y);solve.AddEdge(x, y);solve.AddEdge(y, x);}solve.work(1, n);if (solve.a + solve.b) printf("%d %d\n", solve.a, solve.b);else printf("No solution\n");}return 0;}
温故而知新,又写了一遍,跑的更快了。
#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define ff first#define ss second#define mp(i,j) make_pair(i,j)#define pb push_back#define pii pair<int,LL>#define inone(x) scanf("%d", &x)#define intwo(x,y) scanf("%d%d", &x, &y)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 1e6 + 3;const int N = 2e5 + 10;int n, D, x, y, cas = 0;int ft[N], nt[N], u[N], v[N], sz;int vis[N], mx[N], ct[N];int a1, a2;int inv[mod];int M1[mod],M2[mod];int A[N],B[N],t;int dfs(int x, int fa, int t){int y = mx[x] = (ct[x] = 1) ^ 1;loop(i, ft[x], nt){if (vis[u[i]] || u[i] == fa) continue;int z = dfs(u[i], x, t);ct[x] += ct[u[i]];mx[x] = max(ct[u[i]], mx[x]);y = mx[y] < mx[z] ? y : z;}mx[x] = max(mx[x], t - ct[x]);return mx[x] < mx[y] ? x : y;}void make(int x,int y){    if (!a1) a1=x,a2=y;    if (a1>x) a1=x,a2=y;    if (a1==x&&a2>y) a2=y;}void gao(int x,int fa,int V){    A[t]=V; B[t++]=x;    loop(i,ft[x],nt)    {        if (u[i]==fa||vis[u[i]]) continue;        gao(u[i],x,1LL*V*v[u[i]]%mod);    }}int g(int x){    return inv[1LL*x*inv[D]%mod];}void solve(int x){    M1[v[x]]=x; M2[v[x]]=++cas;    loop(i,ft[x],nt)    {        if (vis[u[i]]) continue;        t=0; gao(u[i],x,v[u[i]]);        rep(j,0,t-1)        {            if (M2[g(A[j])]==cas)            {                int l=B[j],r=M1[g(A[j])];                make(min(l,r),max(l,r));            }        }        rep(j,0,t-1)        {            int r=1LL*A[j]*v[x]%mod;            if (M2[r]==cas) M1[r]=min(M1[r],B[j]);            else M1[r]=B[j],M2[r]=cas;        }    }}void work(int r, int t){int x = dfs(r, 0, t);solve(x); vis[x] = 1;loop(i, ft[x], nt){if (vis[u[i]]) continue;work(u[i], ct[u[i]] < t ? ct[u[i]] : t - ct[x]);}}int main(){    inv[1]=1;    rep(i,2,mod-1) inv[i]=1LL*inv[mod%i]*(mod-mod/i)%mod;while (~intwo(n,D)){mx[0] = INF; sz = 0;a1=a2=0;rep(i, 1, n) vis[i] = 0, ft[i] = -1, inone(v[i]);rep(i, 1, n - 1){intwo(x, y);u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++;u[sz] = x; nt[sz] = ft[y]; ft[y] = sz++;}if (D) work(1, n);if (a1+a2) printf("%d %d\n", a1,a2);else printf("No solution\n");}return 0;}


0 0
原创粉丝点击