标签:
昨天水了一发 poj3764 :http://poj.org/problem?id=3764
写了一份感觉没问题的代码,本地测试ok的,交上去无限WA,代码如下
1 /* 2 * Problem: 3 * Author: SHJWUDP 4 * Created Time: 2015/10/10 星期六 22:05:21 5 * File Name: 1001.cpp 6 * State: 7 * Memo: 8 */ 9 #include <iostream> 10 #include <cstdio> 11 #include <vector> 12 #include <cstring> 13 #include <algorithm> 14 15 using namespace std; 16 17 struct Graph { 18 struct Edge { 19 int u, v, w; 20 Edge(int _u, int _v, int _w):u(_u),v(_v),w(_w){} 21 }; 22 int n, m; 23 vector<Edge> edges; 24 vector<vector<int> > G; 25 vector<int> pw; 26 Graph(int _n):n(_n),m(0),G(_n),pw(_n){} 27 void addEdge(int u, int v, int w) { 28 edges.push_back(Edge(u,v,w)); 29 m=edges.size(); 30 G[u].push_back(m-1); 31 } 32 vector<int> & operator[](int x) { 33 return G[x]; 34 } 35 }; 36 37 struct Trie { 38 struct Node { 39 static const int SIGMA=2; 40 vector<int> ch; 41 Node():ch(SIGMA, -1){} 42 }; 43 vector<Node> tree; 44 Trie():tree(1){} 45 int newNode() { 46 tree.push_back(Node()); 47 return tree.size()-1; 48 } 49 void insert(int x) { 50 int p=0; 51 for(int k=30; k>=0; --k) { 52 int & nt=tree[p].ch[!!(x & (1<<k))]; 53 if(nt==-1) nt=newNode(); 54 p=nt; 55 } 56 } 57 int query(int x) { 58 int ret=0; 59 int p=0; 60 for(int k=30; k>=0; --k) { 61 int tmp=!!(x&(1<<k)); 62 if(tree[p].ch[!tmp]!=-1) { 63 ret|=1<<k; 64 p=tree[p].ch[!tmp]; 65 } else if(tree[p].ch[tmp]!=-1) { 66 p=tree[p].ch[tmp]; 67 } 68 } 69 return ret; 70 } 71 }; 72 73 int n; 74 void dfs(Graph & G, int u, int fa, int val) { 75 G.pw[u]=val; 76 for(int i=0; i<(int)G[u].size(); ++i) { 77 const Graph::Edge & e=G.edges[G[u][i]]; 78 if(e.v==fa) continue; 79 dfs(G, e.v, u, val^e.w); 80 } 81 } 82 int main() { 83 #ifndef ONLINE_JUDGE 84 freopen("in", "r", stdin); 85 // freopen("out", "w", stdout); 86 #endif 87 while(~scanf("%d", &n)) { 88 Graph tree(n); 89 for(int i=0; i<n-1; ++i) { 90 int a, b, c; 91 scanf("%d%d%d", &a, &b, &c); 92 tree.addEdge(a, b, c); 93 tree.addEdge(b, a, c); 94 } 95 dfs(tree, 0, -1, 0); 96 Trie trie; 97 for(int i=0; i<n; ++i) { 98 trie.insert(tree.pw[i]); 99 } 100 int ans=0; 101 for(int i=0; i<n; ++i) { 102 ans=max(ans, trie.query(tree.pw[i])); 103 } 104 printf("%d\n", ans); 105 } 106 return 0; 107 }
错误原因:
newNode调用了push_back,这会使指向vector的迭代器、指针和引用失效,代码修改如下(超时什么的就不要管了=w=
1 /* 2 * Problem: 3 * Author: SHJWUDP 4 * Created Time: 2015/10/10 星期六 22:05:21 5 * File Name: 1001.cpp 6 * State: 7 * Memo: 8 */ 9 #include <iostream> 10 #include <cstdio> 11 #include <vector> 12 #include <cstring> 13 #include <algorithm> 14 15 using namespace std; 16 17 struct Graph { 18 struct Edge { 19 int u, v, w; 20 Edge(int _u, int _v, int _w):u(_u),v(_v),w(_w){} 21 }; 22 int n, m; 23 vector<Edge> edges; 24 vector<vector<int> > G; 25 vector<int> pw; 26 Graph(int _n):n(_n),m(0),G(_n),pw(_n){} 27 void addEdge(int u, int v, int w) { 28 edges.push_back(Edge(u,v,w)); 29 m=edges.size(); 30 G[u].push_back(m-1); 31 } 32 vector<int> & operator[](int x) { 33 return G[x]; 34 } 35 }; 36 37 struct Trie { 38 struct Node { 39 static const int SIGMA=2; 40 vector<int> ch; 41 Node():ch(SIGMA, -1){} 42 }; 43 vector<Node> tree; 44 Trie():tree(1){} 45 int newNode() { 46 tree.push_back(Node()); 47 return tree.size()-1; 48 } 49 int next(int & x) { 50 int ret=x; 51 if(x==-1) { 52 ret=x=tree.size(); 53 tree.push_back(Node()); 54 } 55 return ret; 56 } 57 void insert(int x) { 58 int p=0; 59 for(int k=30; k>=0; --k) { 60 p=next(tree[p].ch[!!(x&(1<<k))]); 61 } 62 } 63 int query(int x) { 64 int ret=0; 65 int p=0; 66 for(int k=30; k>=0; --k) { 67 int tmp=!!(x&(1<<k)); 68 if(tree[p].ch[!tmp]!=-1) { 69 ret|=1<<k; 70 p=tree[p].ch[!tmp]; 71 } else if(tree[p].ch[tmp]!=-1) { 72 p=tree[p].ch[tmp]; 73 } 74 } 75 return ret; 76 } 77 }; 78 79 int n; 80 void dfs(Graph & G, int u, int fa, int val) { 81 G.pw[u]=val; 82 for(int i=0; i<(int)G[u].size(); ++i) { 83 const Graph::Edge & e=G.edges[G[u][i]]; 84 if(e.v==fa) continue; 85 dfs(G, e.v, u, val^e.w); 86 } 87 } 88 int main() { 89 #ifndef ONLINE_JUDGE 90 freopen("in", "r", stdin); 91 // freopen("out", "w", stdout); 92 #endif 93 while(~scanf("%d", &n)) { 94 Graph tree(n); 95 for(int i=0; i<n-1; ++i) { 96 int a, b, c; 97 scanf("%d%d%d", &a, &b, &c); 98 tree.addEdge(a, b, c); 99 tree.addEdge(b, a, c); 100 } 101 dfs(tree, 0, -1, 0); 102 Trie trie; 103 for(int i=0; i<n; ++i) { 104 trie.insert(tree.pw[i]); 105 } 106 int ans=0; 107 for(int i=0; i<n; ++i) { 108 ans=max(ans, trie.query(tree.pw[i])); 109 } 110 printf("%d\n", ans); 111 } 112 return 0; 113 }
标签:
原文地址:http://www.cnblogs.com/shjwudp/p/4869154.html