首页 > 其他分享 >POJ - 3764 XOR&&dfs 01字典树

POJ - 3764 XOR&&dfs 01字典树

时间:2023-04-24 23:03:51浏览次数:30  
标签:01 3764 int dfs long ans xor include define

In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:

{xor}length§=\oplus{e \in p}w(e)
⊕ is the xor operator.

We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?

Input
The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.

Output
For each test case output the xor-length of the xor-longest path.
Sample Input
4
0 1 3
1 2 4
1 3 6
Sample Output
7
Hint
The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

先dfs 出根节点到各个节点的 xor 和,然后就是在这样的一个序列中选出两个xor最大;
那么字典树即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 6000005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-6
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
	ll ans = 1;
	a = a % mod;
	while (b > 0) {
		if (b % 2)ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}

int d[maxn][2], cnt;
struct edge {
	int to, w, nxt;
	edge(){}
	edge(int x,int y,int z):to(x),w(y),nxt(z){}

}e[500005];

int num[200005], vis[200005], tot, head[200005];
void addedge(int from, int to, int w) {
	e[tot] = edge(to, w, head[from]);
	head[from] = tot++;
}

void dfs(int v, int w) {
	vis[v] = 1;
	num[v] = w;
	for (int i = head[v]; i != -1; i = e[i].nxt) {
		if (!vis[e[i].to]) {
			dfs(e[i].to, w^e[i].w);
		}
	}
}

void add(int x) {
	int p = 1;
	for (int i = 30; i >= 0; i--) {
		if (d[p][(x >> i) & 1] == 0)
			d[p][(x >> i) & 1] = ++cnt;
		p = d[p][(x >> i) & 1];
	}
}

int fid(int x) {
	int p = 1, ans = 0;
	for (int i = 30; i >= 0; i--) {
		int t = (x >> i) & 1;
		if (d[p][t ^ 1]) {
			ans += (1 << i);
			p = d[p][t ^ 1];
		}
		else p = d[p][t];
	}
	return ans;
}
int main()
{
	//ios::sync_with_stdio(false);
	int n;
	while (scanf("%d",&n)!=EOF) {
		tot = 0;
		ms(d); ms(vis);
		memset(head, -1, sizeof(head));
		cnt = 1;
		for (int i = 1; i < n; i++) {
			int x, y, w;
			//cin >> x >> y >> w;
			scanf("%d%d%d", &x, &y, &w);
			addedge(x, y, w); addedge(y, x, w);
		}
		dfs(0, 0);
		int ans = 0;
		for (int i = 0; i < n; i++) {
			add(num[i]);
			ans = max(ans, fid(num[i]));
		}
		cout << ans << endl;
	}
}

标签:01,3764,int,dfs,long,ans,xor,include,define
From: https://blog.51cto.com/u_15657999/6222002

相关文章

  • 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)C - Counting Stars 暴力三元环
    LittleAisanastronomylover,andhehasfoundthattheskywassobeautiful!Soheiscountingstarsnow!Therearenstarsinthesky,andlittleAhasconnectedthembymnon-directionaledges.Itisguranteedthatnoedgesconnectonestarwithi......
  • ECNA 2017 Problem G: A Question of Ingestion DP
    StanFordisatypicalcollegegraduatestudent,meaningthatoneofthemostimportantthingsonhismindiswherehisnextmealwillbe.Fortunehassmiledonhimashe’sbeeninvitedtoamulti-coursebarbecueputonbysomeofthecorporatesponsors......
  • CVE-2015-5254漏洞复现
    1.漏洞介绍。ApacheActiveMQ是美国阿帕奇(Apache)软件基金会所研发的一套开源的消息中间件,它支持Java消息服务,集群,SpringFramework等。ApacheActiveMQ5.13.0之前5.x版本中存在安全漏洞,该漏洞源于程序没有限制可在代理中序列化的类。远程攻击者可借助特制的序列化的Java......
  • ACM International Collegiate Programming Contest, Amman Collegiate Programming C
    Youaregivenan × mgrid,yourgoalistofindagroupoflinessuchthatthefollowingconditionsaremet:Notwolinesaretouching.Eachcellinthegridhasoneofitssidescoveredbyatleastonelineinthegroup.Alineisaborderofacellin......
  • Nordic Collegiate Programming Contest (NCPC) 2017 C 在线查询,更新
    Onehundredyearsfromnow,in2117,theInternationalCollegiateProgrammingContest(ofwhichtheNCPCisapart)hasexpandedsignificantlyanditisnowtheGalacticCollegiateProgrammingContest(GCPC).Thisyeartherearenteamsinthecontest.T......
  • The 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
    ThetunnelsofCuChiareanimmensenetworkofundergroundtunnelsconnectingroomslocatedintheCuChiDistrictofHoChiMinhCity.TheCuChitunnelswerethelocationofseveralmilitarycampaignsinthe1960s.Nowadays,itisapopulartouristdes......
  • ACM International Collegiate Programming Contest 2014 B SPFA 统计路径
    FloweryTrails!”()”*+,-).%”/)’0”122”1!2”342”522”!22”652”!42”72”72”5!2”!12”622”18!”162”!12”6”7”4”9”3”5”8”1”2”Inordertoattractmorevisitors,themanagerofanationalp......
  • ACM International Collegiate Programming Contest 2014 A dfs 好题
    GREAT+SWERC=PORTOWewanttohaveagreatSWERCatPortothisyearandweapproachedthischallengeinseveralways.Weevenframeditasawordadditionproblem,similartotheclassicSEND+MORE=MONEY,whereeachletterstandsforasingledigit(......
  • JavaSE学习笔记——01
    Java笔记基础仅仅学习,不涉及任何商用1.注释单行注释:以"//"开头多行注释:以"/"开头,以"/"结尾文档注释:以"/**"开头,"*/"结尾。注释中包含一些说明性的文字及一些JavaDoc标签。publicclassHello{publicstaticvoidmain(String[]args){//单行注释......
  • P2671 [NOIP2015 普及组] 求和
    here看到这个条件,想到等差数列,于是假设了1,3,5位置上的颜色一样时,总和是多少,然后发现是:(1+1+3+5)f(1)+(1+3+3+5)f(3)+(1+3+5+5)f(5)现在看的很清楚了,有两种可能:(i+配对的数之和+i)f(i)或者(i*配对的数的个数+配对的数之和)f(i)。看看样例1,发现后......