Description
Given a graph of n vertex m edges (vertex numbered,..., N) with weights on each edge. The ownership value can be decomposed into 2^a*3^b
The form. Now there are Q queries, each time you ask for a given four parameters U, V, a and B, please find out if there is a path between the vertex u to V, making
The least common multiple of the weighted value on the edge of the path is 2^a*3^b. Note: The path may not be a simple path. Here are some definitions that might be useful
: least common multiple: K number A1,A2,..., ak Least common multiple is the smallest positive integer that can be divisible by each AI. Path: path P:P1,P2,..., PK is the top
Point sequence, satisfies for any 1<=i<k, the node pi and the pi+1 have the edge to connect. Simple path: If the path p:p1,p2,..., PK, for any 1
<=s≠t<=k all have ps≠pt, then the path is called a simple path.
Input
The first line of the input file contains two integers n and m, representing the number of vertices and sides of the graph, respectively. Next m line, each line contains four integers u, V, a,
b represents a vertex between U and V, with a weight of 2^a*3^b edge. The next line contains an integer q, which represents the number of queries. Next Q line, each row contains four
integers u, V, a, and b represent a single query. Please refer to the question description for more information. 1<=n,q<=50000, 1<=m<=100000, 0<=a,b<=10^9
Output
For each query, if there is a path that satisfies the condition, the output line is yes, otherwise the output is one line No (note: The first letter is capitalized, the remaining
Letter lowercase).
Sample Input4 5
1 2 1 3
1 3 1 2
1 4 2 1
2 4 3 2
3 4 2 2
5
1 4 3 3
4 2 2 3
1 3 2 2
2 3 2 2
1 3 4 4Sample OutputYes
Yes
Yes
No
No
Exercises
http://blog.csdn.net/thy_asdf/article/details/51203421
Note that the collection cannot be compressed by the path, and it should be combined with heuristic
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Charch;8 BOOLOK;9 voidReadint&x) {Ten for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; One for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); A if(OK) x=-x; - } - Const intmaxn=150005; the intN,M,Q,RM,TOT,CNT,FA[MAXN],SIZ[MAXN],MAXA[MAXN],MAXB[MAXN]; - intFindintx) {returnX==FA[X]?X:find (fa[x]);} - BOOLANS[MAXN]; - structdata{ + intU,v,a,b,id; - voidInitinti) {read (U), read (v), read (a), read (b), id=i;} + }EDGE[MAXN],QUER[MAXN],TMP[MAXN]; A BOOLCmpaConstData &x,ConstData &y) {returnx.a<y.a| | (x.a==y.a&&x.b<y.b);} at BOOLCMPB (ConstData &x,ConstData &y) {returnx.b<y.b| | (x.b==y.b&&x.a<y.a);} - structoper{ - intU,V,FA,SIZ,MA,MB; - }OPER[MAXN]; - voidMergeintUintVintAintb) { -U=find (u), v=Find (v); in if(siz[u]>Siz[v]) swap (U,V); -oper[++cnt]=(Oper) {u,v,fa[u],siz[v],maxa[v],maxb[v]}; to if(u==v) Maxa[u]=max (maxa[u],a), maxb[u]=Max (maxb[u],b); + ElseFa[u]=v,siz[v]+=siz[u],maxa[v]=max (Maxa[v],max (maxa[u],a)), maxb[v]=Max (Maxb[v],max (maxb[u],b)); - } the voidundo () { * for(; cnt;cnt--){ $fa[oper[cnt].u]=Oper[cnt].fa;Panax Notoginsengmaxa[oper[cnt].v]=oper[cnt].ma; -maxb[oper[cnt].v]=OPER[CNT].MB; thesiz[oper[cnt].v]=Oper[cnt].siz; + } A } the intMain () { +Read (n), read (m), rm=sqrt (m); - for(intI=1; i<=m;i++) Edge[i].init (i); $Sort (edge+1, edge+m+1, CMPA); $ read (q); - for(intI=1; i<=q;i++) Quer[i].init (i); -Sort (quer+1, quer+q+1, CMPB); the for(intI=1; i<=m;i+=RM) { -tot=0;Wuyi for(intj=1; j<=q;j++)if(edge[i].a<=quer[j].a&& (i+rm>m| | QUER[J].A<EDGE[I+RM].A)) tmp[++tot]=Quer[j]; theSort (edge+1, edge+I,CMPB); - for(intj=1; j<=n;j++) fa[j]=j,siz[j]=1, maxa[j]=maxb[j]=-1; Wu for(intj=1, k=1; j<=tot;j++){ - for(; k<i&&edge[k].b<=tmp[j].b;k++) Merge (edge[k].u,edge[k].v,edge[k].a,edge[k].b); AboutCnt=0; $ for(intp=i;p<i+rm&&p<=m;p++)if(edge[p].a<=tmp[j].a&&edge[p].b<=tmp[j].b) - merge (edge[p].u,edge[p].v,edge[p].a,edge[p].b); - intX=find (tmp[j].u), y=find (TMP[J].V); -Ans[tmp[j].id]= (x==y&&maxa[x]==tmp[j].a&&maxb[x]==tmp[j].b); A undo (); + } the } - for(intI=1; i<=q;i++)if(Ans[i]) puts ("Yes");ElsePuts"No"); $ return 0; the}
4537: [Hnoi2016] least common multiple