ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
题意:一棵树上有m个红色结点,树的边有权值。q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化。q次查询相互独立。
分析:数据量很... 最大值最小化,二分搜答案。将ST表求lca的dfs函数加一点东西,求出每个点到其最近红色祖先的距离。check函数中,枚举到其红色祖先的距离超过下限的点。设非法点数为p,尝试将其距离通过一次染色缩小。具体做法是求出这p个点的lca,这个点就是要染红的点,因为只有修改这个点,才有可能将所有点的红祖先距离都缩减。若修改之后仍有结点非法,则check失败
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=100005;
struct node{
	int v,next;
	LL dis;
}edges[N<<1];
int head[N],e;
int id[N]; //节点第一次被遍历的顺序
LL dis[N]; //节点到根节点的距离
LL rdis[N]; //距离最近的红色祖先的距离
bool red[N];
int RMQ[N*2][20];
int curID;
//F[i]表示第i个遍历的节点
//B[i]表示F[i]在树中的深度
int F[N*2],B[N*2];
int n,m,Q,root;
#include<bits/stdc++.h>
using namespace std;
////////////////
namespace IO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
    //fread->read
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if (p1 == pend) {
            p1 = buf; pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if (pend == p1) { IOerror = 1; return -1; }
            //{printf("IO error!\n");system("pause");for (;;);exit(0);}
        }
        return *p1++;
    }
    inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
    inline void read(int &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for (; blank(ch); ch = nc());
        if (IOerror)return;
        if (ch == '-')sign = 1, ch = nc();
        for (; ch >= '0'&&ch <= '9'; ch = nc())x = x * 10 + ch - '0';
        if (sign)x = -x;
    }
    inline void read(ll &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for (; blank(ch); ch = nc());
        if (IOerror)return;
        if (ch == '-')sign = 1, ch = nc();
        for (; ch >= '0'&&ch <= '9'; ch = nc())x = x * 10 + ch - '0';
        if (sign)x = -x;
    }
    inline void read(double &x) {
        bool sign = 0; char ch = nc(); x = 0;
        for (; blank(ch); ch = nc());
        if (IOerror)return;
        if (ch == '-')sign = 1, ch = nc();
        for (; ch >= '0'&&ch <= '9'; ch = nc())x = x * 10 + ch - '0';
        if (ch == '.') {
            double tmp = 1; ch = nc();
            for (; ch >= '0'&&ch <= '9'; ch = nc())tmp /= 10.0, x += tmp * (ch - '0');
        }
        if (sign)x = -x;
    }
    inline void read(char *s) {
        char ch = nc();
        for (; blank(ch); ch = nc());
        if (IOerror)return;
        for (; !blank(ch) && !IOerror; ch = nc())*s++ = ch;
        *s = 0;
    }
    inline void read(char &c) {
        for (c = nc(); blank(c); c = nc());
        if (IOerror) { c = -1; return; }
    }
    //fwrite->write
    struct Ostream_fwrite {
        char *buf, *p1, *pend;
        Ostream_fwrite() { buf = new char[BUF_SIZE]; p1 = buf; pend = buf + BUF_SIZE; }
        void out(char ch) {
            if (p1 == pend) {
                fwrite(buf, 1, BUF_SIZE, stdout); p1 = buf;
            }
            *p1++ = ch;
        }
        void print(int x) {
            static char s[15], *s1; s1 = s;
            if (!x)*s1++ = '0'; if (x<0)out('-'), x = -x;
            while (x)*s1++ = x % 10 + '0', x /= 10;
            while (s1-- != s)out(*s1);
        }
        void println(int x) {
            static char s[15], *s1; s1 = s;
            if (!x)*s1++ = '0'; if (x<0)out('-'), x = -x;
            while (x)*s1++ = x % 10 + '0', x /= 10;
            while (s1-- != s)out(*s1); out('\n');
        }
        void print(ll x) {
            static char s[25], *s1; s1 = s;
            if (!x)*s1++ = '0'; if (x<0)out('-'), x = -x;
            while (x)*s1++ = x % 10 + '0', x /= 10;
            while (s1-- != s)out(*s1);
        }
        void println(ll x) {
            static char s[25], *s1; s1 = s;
            if (!x)*s1++ = '0'; if (x<0)out('-'), x = -x;
            while (x)*s1++ = x % 10 + '0', x /= 10;
            while (s1-- != s)out(*s1); out('\n');
        }
        void print(double x, int y) {
            static ll mul[] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000,
                1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
                100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL };
            if (x<-1e-12)out('-'), x = -x; x *= mul[y];
            ll x1 = (ll)floor(x); if (x - floor(x) >= 0.5)++x1;
            ll x2 = x1 / mul[y], x3 = x1 - x2 * mul[y]; print(x2);
            if (y>0) { out('.'); for (size_t i = 1; i<y&&x3*mul[i]<mul[y]; out('0'), ++i); print(x3); }
        }
        void println(double x, int y) { print(x, y); out('\n'); }
        void print(char *s) { while (*s)out(*s++); }
        void println(char *s) { while (*s)out(*s++); out('\n'); }
        void flush() { if (p1 != buf) { fwrite(buf, 1, p1 - buf, stdout); p1 = buf; } }
        ~Ostream_fwrite() { flush(); }
    }Ostream;
    inline void print(int x) { Ostream.print(x); }
    inline void println(int x) { Ostream.println(x); }
    inline void print(char x) { Ostream.out(x); }
    inline void println(char x) { Ostream.out(x); Ostream.out('\n'); }
    inline void print(ll x) { Ostream.print(x); }
    inline void println(ll x) { Ostream.println(x); }
    inline void print(double x, int y) { Ostream.print(x, y); }
    inline void println(double x, int y) { Ostream.println(x, y); }
    inline void print(char *s) { Ostream.print(s); }
    inline void println(char *s) { Ostream.println(s); }
    inline void println() { Ostream.out('\n'); }
    inline void flush() { Ostream.flush(); }
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace IO;
void init()
{
    e = 0; curID = 0;
    memset(head,-1,sizeof(head));
	memset(red,0,sizeof(red));
}
void AddEdge (int u,int v,LL w)
{
	edges[e].v=v;
	edges[e].dis=w;
	edges[e].next=head[u];
	head[u]=e++;
}
void DFS (int u,int p,int Dep,LL d)
{
	int i,v;
	if(red[u]) d=0;
	curID++;
	rdis[u] = d;
	F[curID]=u;
	B[curID]=Dep;
	id[u]=curID;
	for (i=head[u];i!=-1;i=edges[i].next){
		v=edges[i].v;
		if (v==p) continue;
		dis[v]=dis[u] + edges[i].dis;
		DFS(v,u,Dep+1,d+edges[i].dis);
		curID++;
		F[curID]=u;
		B[curID]=Dep;
	}
}
void initRMQ ()
{
	int i,j,x,y;
	for (i=1;i<=curID;i++)
		RMQ[i][0]=i;
	for (j=1;(1<<j)<=curID;j++)
		for (i=1;i+(1<<j)-1<=curID;i++){
			x=RMQ[i][j-1];
			y=RMQ[i+(1<<(j-1))][j-1];
			RMQ[i][j]=B[x]<B[y]?x:y;
		}
}
int getLCA (int a,int b)
{
	int k,x,y;
	a=id[a];b=id[b];
	if (a>b)
		k=a,a=b,b=k;
	k = 31 - __builtin_clz(b-a+1);
	x=RMQ[a][k];
	y=RMQ[b-(1<<k)+1][k];
	return B[x]<B[y]?F[x]:F[y];
}
int qq[N],cnt;
int vz[N];
bool check(LL &x)
{
	int all =0;
	for(int i=0;i<cnt;++i){             //枚举超过限制的结点
		if(rdis[qq[i]]>x){
			vz[all++] = qq[i];
		}
	}
	if(all==0) return true;
	int lca = vz[0];
	for(int i=0;i<all;++i){             //求出所有非法结点的lca,这个lca就是要染色的点
		lca = getLCA(lca,vz[i]);
	}
	LL mx = 0;
	for(int i=0;i<all;++i){             //查看最长距离
        if(dis[vz[i]]-dis[lca]>x) return false;
	}
	return true;
}
int main()
{
	#ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T; read(T);
    while(T--){
		int n,M,Q;
		int u,v; LL w;
		LL up = 0;
        init();
		read(n), read(M), read(Q);
        //scanf("%d %d %d",&n,&M,&Q);
		for(int i=1;i<=M;++i){
			read(u);
			red[u] = 1;
        }
		red[1] = 1;
        for(int i=1;i<n;++i){
			//scanf("%d %d %lld",&u,&v,&w);
			read(u), read(v), read(w);
			AddEdge(u,v,w);
			AddEdge(v,u,w);
			up += w;
        }
		DFS(1,0,0,0);
		initRMQ();
		while(Q--){
			int k; 	read(k);//scanf("%d",&k);
			cnt = k;
			for(int i=0;i<k;++i){
				read(qq[i]);
				//scanf("%d",&qq[i]);
			}
			LL L =0,R = up,mid,ans = up;
			while(L<=R){
				mid = (L+R)>>1;
				if(check(mid)){
					ans = mid;
					R = mid-1;
				}
				else L =mid+1;
			}
			println(ans);
			//printf("%lld\n",ans);
		}
    }
    return 0;
}
ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online的更多相关文章
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
		Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ... 
- HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
		Description A new Semester is coming and students are troubling for selecting courses. Students ... 
- The 2019 ICPC Asia Shanghai Regional Contest H  Tree Partition  k、Color Graph
		H题意: 给你一个n个节点n-1条无向边构成的树,每一个节点有一个权值wi,你需要把这棵树划分成k个子树,每一个子树的权值是这棵子树上所有节点权值之和. 你要输出这k棵子树的权值中那个最大的.你需要让 ... 
- HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)
		Problem Description Zero and One are good friends who always have fun with each other. This time, ... 
- ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]
		题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ... 
- ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]
		题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ... 
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ... 
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
		题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ... 
- HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)
		Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ... 
随机推荐
- phpredis 中文手册和redis 教程
			phpredis 中文手册 : http://www.cnblogs.com/zcy_soft/archive/2012/09/21/2697006.html 手册: http://www.cn ... 
- 【黑金原创教程】 FPGA那些事儿 SignalTap II 调试技巧
			简介工具篇系列的第三本教程,讲述各种与SignalTap II 有关的调试技巧. 目录[黑金原创教程] FPGA那些事儿<工具篇III>:File01 - 上线调试与下线调试[黑金原创教程 ... 
- oracle11g配置dataguard
			DATAGUARD是通过建立一个PRIMARY和STANDBY组来确立其参照关系. STANDBY一旦创建,DATAGUARD就会通过将主数据库(PRIMARY)的REDO传递给STAND ... 
- ios UITableView多选删除
			第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ... 
- linux下如何启动nginx?
			命令: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ........... [root@localhost sbin ... 
- R语言中聚类确定最佳K值之Calinsky criterion
			Calinski-Harabasz准则有时称为方差比准则 (VRC),它可以用来确定聚类的最佳K值.Calinski Harabasz 指数定义为: 其中,K是聚类数,N是样本数,SSB是组与组之间的 ... 
- Codeforces Round #426 (Div. 2)A题&&B题&&C题
			A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ... 
- Truncate有外键约束的表
			SET FOREIGN_KEY_CHECKS=0; TRUNCATE TABLE table_name; SET FOREIGN_KEY_CHECKS=1; 
- Cookies and Caching  Client Identification
			w HTTP The Definitive Guide 11.6.9 Cookies and Caching You have to be careful when caching documents ... 
- golang处理 json中非法字符
			原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ... 
