D Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 5122 Accepted Submission(s): 1072
Problem 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?
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.
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.
For more information, please refer to the Sample Output below.
Sample Input
5 60 2 5 2 3 3 1 2 1 3 2 4 2 5 5 2 2 5 2 3 3 1 2 1 3 2 4 2 5
Sample Output
3 4 No solutionHint1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较,若第一个数字大小相同,则按照第二个数字大小进行比较,依次类推。 2. 若出现栈溢出,推荐使用C++语言提交,并通过以下方式扩栈: #pragma comment(linker,"/STACK:102400000,102400000")
Source
Recommend
liuyiding
题意:给出一棵树,寻找一条路径,使得路径上的点相乘取模后等于k,输出路径的两个端点,输出字典序最小的
解题思路:树分治
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
const int mod = 1e6 + 3;
int n, K, a[maxn];
int s[maxn], nt[maxn], e[maxn], v[maxn], vis[maxn];
int cnt[maxn], mx[maxn], inv[mod], x[mod], ans1, ans2;
set<int>ss;
set<int>::iterator it;
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 dfs(int k, int fa, int sum)
{
int ans = mx[k] = 0;
cnt[k] = 1;
for (int i = s[k]; i != -1; i = nt[i])
{
if (vis[e[i]] || e[i] == fa) continue;
int y = dfs(e[i], k, sum);
if (mx[y] < mx[ans]) ans = y;
cnt[k] += cnt[e[i]];
mx[k] = max(mx[k], cnt[e[i]]);
}
mx[k] = max(mx[k], sum - cnt[k]);
return mx[k] < mx[ans] ? k : ans;
}
void get(int k, int fa, int p)
{
int y = (LL)K*inv[p] % mod;
it = ss.find(y);
if (it != ss.end())
{
if (!ans1) ans1 = min(x[y], k), ans2 = max(x[y], k);
else
{
if (ans1 > min(x[y], k)) ans1 = min(x[y], k), ans2 = max(x[y], k);
else if (ans1 == min(x[y], k) && ans2 > max(x[y], k)) ans2 = max(x[y], k);
}
}
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]] || fa == e[i]) continue;
get(e[i], k, (LL)p*a[e[i]] % mod);
}
}
void put(int k, int fa, int p)
{
it = ss.find(p);
if (it != ss.end()) x[p] = min(x[p], k);
else ss.insert(p), x[p] = k;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]] || fa == e[i]) continue;
put(e[i], k, (LL)p*a[e[i]] % mod);
}
}
void Find(int k)
{
ss.clear(); ss.insert(1);
x[1] = k;
for (int i = s[k]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
get(e[i], k, (LL)a[e[i]] * a[k] % mod);
put(e[i], k, a[e[i]]);
}
}
void work(int k, int sum)
{
int y = dfs(k, k, sum);
vis[y] = 1, Find(y);
for (int i = s[y]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
if (cnt[e[i]] < cnt[y]) work(e[i], cnt[e[i]]);
else work(e[i], sum - cnt[y]);
}
}
int main()
{
init();
while (~scanf("%d %d", &n, &K))
{
int cnt = 0;
ans1 = ans2 = 0;
mx[0] = INF;
memset(s, -1, sizeof s);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
nt[cnt] = s[u], s[u] = cnt, e[cnt++] = v;
nt[cnt] = s[v], s[v] = cnt, e[cnt++] = u;
}
work(1, n);
if (ans1) printf("%d %d\n", ans1, ans2);
else printf("No solution\n");
}
return 0;
}