hdu 4812 multik

D Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4647    Accepted Submission(s): 925


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?
 

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 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")
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6032  6031  6030  6029  6028 
 






【分析】
首先预处理逆元(这个我也不知道咋回事,就当是黑箱哈,坑以后再填咯)
然后点分治...map[i]表示乘积为i的路径的字典序最小端点。注意端点为重心的路径。

突然发现我自带大常数和大代码复杂度QAQ


【代码】
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1e9+7
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int p=1e6+3;
const int mxn=200005;
bool vis[mxn];
int n,K,mn,cnt,tot,lef,rig,root,mxdep;
struct edge {int to,next;} f[mxn<<1];
int head[mxn],size[mxn],mx[mxn],map[p+3];
ll val[mxn],inv[p+3],dis[mxn];
inline void init()
{
	inv[1]=1;
    for(int i=2;i<p;i++)
    {
        int a=p/i,b=p%i;
        inv[i]=(inv[b]*(-a)%p+p)%p;
    }
}
inline void add(int u,int v)
{
	f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline void dfssize(int u,int fa)
{
	size[u]=1,mx[u]=0;
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v] || v==fa) continue;
		dfssize(v,u);
		size[u]+=size[v];
		mx[u]=max(mx[u],size[v]);
	}
}
inline void dfsroot(int r,int u,int fa)
{
	mx[u]=max(mx[u],size[r]-size[u]);
	if(mx[u]<mn) mn=mx[u],root=u;
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v] || v==fa) continue;
		dfsroot(r,v,u);
	}
}
inline void dfsdis(int u,int fa)
{
	ll tmp1=dis[u]*val[root]%p,tmp2=inv[tmp1]*K%p;
	if(map[tmp2]<=n && map[tmp2]>=1)
	{
		int l=u,r=map[tmp2];
		if(l>r) swap(l,r);
		if(l<lef || (l==lef && r<rig)) lef=l,rig=r;
	}
	if(tmp1==K)
	{
		int l=u,r=root;
		if(l>r) swap(l,r);
		if(l<lef || (l==lef && r<rig)) lef=l,rig=r;
	}
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v] || v==fa) continue;
		dis[v]=dis[u]*val[v]%p;
		dfsdis(v,u);
	}
}
inline void update(int u,int fa)
{
	map[dis[u]]=min(map[dis[u]],u);
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v] || v==fa) continue;
		update(v,u);
	}
}
inline void erase(int u,int fa)
{
	map[dis[u]]=inf;
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v] || v==fa) continue;
		erase(v,u);
	}
}
inline void calc(int u)
{
	for(int i=head[u];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v]) continue;
		dis[v]=val[v];
		dfsdis(v,u);
		update(v,u);
	}
	erase(u,0);
}
inline void dfs(int u)
{
	mn=n;
	dfssize(u,0);
	dfsroot(u,u,0);
	calc(root);
	vis[root]=1;
	for(int i=head[root];i;i=f[i].next)
	{
		int v=f[i].to;
		if(vis[v]) continue;
		dfs(v);
	}
}
int main()
{
    init();
    int i,j,u,v,w;
    while(scanf("%d%d",&n,&K)!=EOF)
    {
    	memset(map,0x3f,sizeof map); 
    	fo(i,1,n) scanf("%lld",&val[i]);
    	M(head),M(vis),cnt=0,lef=rig=n+1;
    	fo(i,2,n)
    	{
    		scanf("%d%d",&u,&v);
    		add(u,v),add(v,u);
		}
		dfs(1);
		if(lef>n) printf("No solution\n");
		else printf("%d %d\n",lef,rig);
	}
	return 0;
}
/*
5 4
2 5 2 3 3
1 2
1 3
2 4
2 5
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值