| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求测试数据,或者帮忙看下这个java代码, re中,本地随机数据测试,对比ac的cpp代码没有问题import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
new POJ3764().run();
}
}
class POJ3764 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int run() throws NumberFormatException, IOException {
while(true) {
String line = br.readLine();
if(line == null) break;
int nVert = Integer.parseInt(line);
Graph g = new Graph(nVert);
for(int i = 0; i < nVert - 1; i ++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
//String[] tokens = br.readLine().split(" ", -1);
//int u = Integer.parseInt(tokens[0]);
//int v = Integer.parseInt(tokens[1]);
//int w = Integer.parseInt(tokens[2]);
g.addEdge(u, v, w);
g.addEdge(v, u, w);
}
int[] xorDist = new int[nVert];
boolean[] visited = new boolean[nVert];
getDist(g, visited, xorDist, 0, 0);
//for(int i = 0; i < nVert; i ++)
//System.out.println(i + " " + xorDist[i]);
BinaryTree bt = new BinaryTree(31*nVert+100);
int result = 0;
for(int i = 0; i < nVert; i ++) {
bt.insert(xorDist[i]);
result = Math.max(result, bt.getMax(xorDist[i]));
}
System.out.println(result);
}
return 0;
}
void getDist(Graph g, boolean[] visited, int[] xorDist, int now, int xorValue) {
if(visited[now]) return ;
visited[now] = true;
xorDist[now] = xorValue;
for(int e = g.head[now]; e != -1; e = g.next[e]) {
getDist(g, visited, xorDist, g.to[e], g.weight[e]^xorValue);
}
}
}
class BinaryTree {
int np;
int[][] child;
BinaryTree(int size) {
child = new int[2][size];
Arrays.fill(child[0], -1);
Arrays.fill(child[1], -1);
np = 1;
}
int getMax(int mask) {
int result = 0, now = 0;
for(int level = 30; level != -1; level --) {
int next = (mask&(1<<level)) == 0 ? 1 : 0;
if(child[next][now] == -1) {
now = child[1-next][now];
} else {
result += (1<<level);
now = child[next][now];
}
}
return result;
}
void insert(int mask) {
int now = 0;
for(int level = 30; level != -1; level --) {
int idx = (mask&(1<<level)) == 0 ? 0 : 1;
if(child[idx][now] == -1) {
child[idx][now] = np ++;
}
now = child[idx][now];
}
}
}
class Graph {
int ep;
int[] head, to, weight, next;
int nVert;
Graph(int nV) {
nVert = nV;
ep = 0;
head = new int[nVert];
Arrays.fill(head, -1);
to = new int[nVert*2];
weight = new int[nVert*2];
next = new int[nVert*2];
}
public void addEdge(int u, int v, int w) {
weight[ep] = w;
next[ep] = head[u];
to[ep] = v;
head[u] = ep;
ep ++;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator