D Tree

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 106 + 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 <= 105) and K(0 <=K < 106 + 3). The following line contains n numbers vi(1 <= vi < 106 + 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 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 solution
 
Hint

1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较,若第一个数字大小相同,则按照第二个数字大小进行比较,依次类推。

2. 若出现栈溢出,推荐使用C++语言提交,并通过以下方式扩栈:
#pragma comment(linker,"/STACK:102400000,102400000")

 

题意:

  给你一棵树n个点,一个K

  让你找到一条 a->b 的字典数最小的 路径满足 这条路径 上点权 乘积取mod下 等于K

题解:

  预处理小于mod的 所有逆元

  树分治 即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, inf = 2e9, mod = ; int head[N],vis[N],f[N],siz[N],id[N],n,t = ,ansl,ansr,allnode,root; struct edge{int to,next;}e[N * ];
LL mp[M],inv[M],v[M],K,deep[M];
void add(int u,int v) {e[t].next=head[u];e[t].to=v;head[u]=t++;} void getroot(int u,int fa) {
f[u] = ;
siz[u] = ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(vis[to] || to == fa) continue;
getroot(to,u);
siz[u] += siz[to];
f[u] = max(f[u],siz[to]);
}
f[u] = max(f[u], allnode - siz[u]);
if(f[u] < f[root]) root = u;
}
void getdeep(int u,int fa,LL now) {
deep[++deep[]] = now*v[u]%mod;
id[deep[]] = u;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(vis[to] || to == fa) continue;
getdeep(to,u,now*v[u]%mod);
}
}
void update(int u,int x,int y) {
int tmp = mp[inv[x*v[u]%mod]*K%mod];
if(!tmp) return ;
if(y > tmp) swap(y,tmp);
if(y < ansl || (y == ansl && tmp < ansr)) ansl = y, ansr = tmp;
} void work(int u){
vis[u] = ;
mp[] = u;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
deep[] = ;
getdeep(to,u,);
for(int j = ; j <= deep[]; ++j) update(u,deep[j],id[j]);
for(int j = ; j <= deep[]; ++j) if(!mp[deep[j]] || mp[deep[j]] > id[j])mp[deep[j]] = id[j];
}
mp[] = ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
deep[] = ;
getdeep(to,u,);
for(int j = ; j <= deep[]; ++j) mp[deep[j]] = ;
}
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
root = ;
allnode = siz[to];
getroot(e[i].to,root);
work(root);
}
}
int main() {
inv[]=;
for(int i=;i<mod;i++){int a=mod/i,b=mod%i;inv[i]=(inv[b]*(-a)%mod+mod)%mod;}
while(~scanf("%d%I64d",&n,&K)) {
t = ;memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
ansl = ansr = inf;
for(int i = ; i <= n; ++i) scanf("%I64d",&v[i]);
for(int i = ; i < n; ++i) {
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
f[]=inf;
allnode=n;root=;
getroot(,);
work(root);
if(ansl == inf) puts("No solution");else
printf("%d %d\n",ansl,ansr);
}
return ;
}

最新文章

  1. 刷新本地的DNS缓存数据
  2. linux-命令-ls
  3. vs2015 附加到进程找不到w3wp.exe
  4. 深入学习JavaScript: apply 方法 详解(转)——非常好
  5. NK3C开发要点
  6. Python入门笔记(13):列表解析
  7. C语言 文件操作4--文件结构体FILE的理解以及缓冲区再讲
  8. 当sql报错代码,不允许对表操作的原因
  9. js和jquery中的事件委托
  10. Long Dominoes(ZOJ 2563状压dp)
  11. Java线性表的排序
  12. QT5.5实现串口通信
  13. go web 第二天 学习笔记之文件上传
  14. [转载] Spark:大数据的“电光石火”
  15. ASP.NET MVC5+EF6+EasyUI 后台管理系统(89)-EF执行SQL语句与存储过程
  16. 每个前端开发者必会的 20 个 JavaScript 面试题
  17. 卷积神经网络(Convolutional Neural Network, CNN)简析
  18. react面试问题总结
  19. Unix分类学习
  20. error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct

热门文章

  1. oracle,mybatis主键自增长
  2. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
  3. 5. javacript高级程序设计-引用类型
  4. 【转】TextView长按复制实现方法小结
  5. Effective C++ -----条款11: 在operator=中处理“自我赋值”
  6. 2014 12th GDCPC 总结
  7. jquery 中的一写常用方法
  8. Qt Creator 中关于调试器的设置
  9. 让div等块级元素水平以及垂直居中的解决办法
  10. HTML标记语法之列表元素