首页 > 其他分享 >cf1899G. Unusual Entertainment(启发式合并)

cf1899G. Unusual Entertainment(启发式合并)

时间:2023-11-18 23:56:20浏览次数:35  
标签:Unusual puts Entertainment int cf1899G head tot include define

https://codeforces.com/contest/1899/problem/G
首先将将节点重新映射一下
然后就是个启发式合并板题

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<ctime>
#define A puts("YES")
#define B puts("NO")
//#define A puts("Yes")
//#define B puts("No")
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
#define lc (o<<1)
#define rc (o<<1|1)
using namespace std;
//typedef __int128 i128;
typedef double db;
typedef long long ll;
const ll mo=998244353;
const ll inf=1ll<<60;
const int N=2e5+5;
int n,x,y,q,z;
int head[N],to[N*2],nex[N*2],tot,p[N],len;
set<int> c[N];
struct node{
	int l,r,id;
};
vector<node> v[N];
bool ans[N];
void add(int x,int y){
	to[++tot]=y; nex[tot]=head[x]; head[x]=tot;
}
void dfs(int x,int y){
	c[x].clear();
	c[x].insert(p[x]);
	for (int i=head[x];i;i=nex[i]) {
		int v=to[i];
		if (v==y) continue;
		dfs(v,x);
		
		if (c[x].size()<c[v].size()) c[x].swap(c[v]);
		for (auto j:c[v]) c[x].insert(j);
	}
	
	for (int i=0;i<(int)v[x].size();i++) {
		auto it=c[x].lower_bound(v[x][i].l);
		if (it==c[x].end()) {
			ans[v[x][i].id]=0;
		}
		else {
			if ((*it) <=v[x][i].r) ans[v[x][i].id]=1;
			else ans[v[x][i].id]=0;
		}
	}
}
int main()
{
//	freopen("data.in","r",stdin);
//	freopen("ans.out","w",stdout);
	
	int T;
	scanf("%d",&T);
	while (T--){
		scanf("%d %d",&n,&q);
	
		
		tot=0;
		fo(i,1,n) head[i]=0;
		fo(i,1,n-1) {
			scanf("%d %d",&x,&y);
			add(x,y); add(y,x);
		}
		
		fo(i,1,n) {
			v[i].clear();
			scanf("%d",&x);
			p[x]=i;
		}
		
		fo(i,1,q) {
			ans[i]=0;
			
			scanf("%d %d %d",&x,&y,&z);
			v[z].push_back((node){x,y,i});
		}
		
		dfs(1,0);
		
		fo(i,1,q) if (ans[i]) A;
		else B;

		puts("");
	}

	return 0;
} 

  

标签:Unusual,puts,Entertainment,int,cf1899G,head,tot,include,define
From: https://www.cnblogs.com/ganking/p/17841494.html

相关文章

  • Codeforces Round #450 (Div. 2) D. Unusual Sequences 数学
    Countthenumberofdistinctsequencesa1, a2, …, an(1 ≤ ai)consistingofpositiveintegerssuchthatgcd(a1, a2, …, an) = xand.Asthisnumbercouldbelarge,printtheanswermodulo109 + 7.gcdheremeansthegreatestcommondivisor.Input......
  • Codeforces 1322 A. Unusual Competitions
    题意:给出一个含有的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成我们只需要从头开始遍历然后找到这种字符,并且使得和......
  • jcenter(JCEntertainment)
    jcenter里的androidstudio微信sdk是哪个如果你只是要跑起来微信分享的demo,暂时使用它demo里边的debug.keystore就行,具体设置在window-preferences-android-build,在custom......