Permalink
Browse files

add poj 3107

  • Loading branch information...
vmingchen committed Oct 25, 2010
1 parent 8dca19c commit dede3f2bfc7b086c72a64b18eb67285d6b10c51e
Showing with 643 additions and 0 deletions.
  1. +115 −0 3107/godfather.cpp
  2. +125 −0 3107/godfather1.cpp
  3. +135 −0 3107/godfather2.cpp
  4. +136 −0 3107/godfather3.cpp
  5. +132 −0 3107/godfather4.cpp
View
@@ -0,0 +1,115 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: godfather.cpp
+ *
+ * Description: POJ 3107
+ *
+ * Version: 1.0
+ * Created: 10/14/2010 05:44:07 PM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+
+#include <cstdio>
+#include <algorithm>
+
+using namespace std;
+
+const int MAXN = 50000;
+
+struct Edge {
+ int a, b;
+};
+
+int n;
+int len[MAXN], cur[MAXN], beg[MAXN], targets[2*MAXN], weights[2*MAXN], topos[2*MAXN];
+Edge edges[MAXN];
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ scanf("%d", &n);
+ for (int i = 0; i < n-1; ++i) {
+ int a, b;
+ scanf("%d %d", &a, &b);
+ edges[i].a = --a;
+ edges[i].b = --b;
+ ++len[a];
+ ++len[b];
+ }
+
+ for (int i = 1; i < n; ++i) cur[i] = beg[i] = len[i-1] + beg[i-1];
+ for (int i = 0; i < n-1; ++i) {
+ int posa = cur[edges[i].a]++;
+ int posb = cur[edges[i].b]++;
+ targets[posa] = edges[i].b;
+ targets[posb] = edges[i].a;
+ topos[posa] = posb;
+ topos[posb] = posa;
+ }
+
+ int stack[MAXN], count[MAXN], pos[MAXN], trace[MAXN], sp = 0;
+
+ stack[sp] = 0;
+ pos[sp] = 0;
+ count[0] = 1;
+
+ // depth-first visit traversal, stack operations
+ while ( sp >= 0 ) {
+ int src = stack[sp];
+ int parent = sp > 0 ? stack[sp-1] : -1;
+ if ( pos[sp] < len[src] && targets[beg[src]+pos[sp]] == parent ) ++pos[sp];
+ if ( pos[sp] < len[src] ) {
+ stack[sp+1] = targets[beg[src]+pos[sp]];
+ trace[sp+1] = beg[src] + pos[sp];
+ pos[sp+1] = 0;
+ count[stack[sp+1]] = 1;
+ ++pos[sp++];
+ } else {
+ if ( sp > 0 ) {
+ count[parent] += count[src];
+ weights[trace[sp]] = count[src];
+ weights[topos[trace[sp]]] = n - count[src];
+ }
+ --sp;
+ }
+ }
+
+ int result[MAXN], numres = 0;
+ int minsub = n;
+ for (int i = 0; i < n; ++i) {
+ int max = 0;
+ for (int j = 0; j < len[i]; ++j) {
+ if ( weights[beg[i]+j] > max )
+ max = weights[beg[i]+j];
+ }
+ if ( max < minsub ) {
+ numres = 0;
+ minsub = max;
+ result[numres++] = i;
+ } else if ( max == minsub ) {
+ result[numres++] = i;
+ }
+ }
+
+ sort(result, result+numres);
+ printf("%d", result[0]+1);
+ for (int i = 1; i < numres; ++i)
+ printf(" %d", result[i]+1);
+ printf("\n");
+
+ return 0;
+} /* ---------- end of function main ---------- */
View
@@ -0,0 +1,125 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: godfather.cpp
+ *
+ * Description: POJ 3107
+ *
+ * Version: 1.0
+ * Created: 10/14/2010 05:44:07 PM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+
+#include <vector>
+#include <iostream>
+
+using namespace std;
+
+const int MAXN = 50000;
+
+int n;
+vector<int> comm[MAXN];
+int parent[MAXN], stack[MAXN], weight[MAXN];
+size_t pos[MAXN];
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: countsort
+ * Description:
+ * =====================================================================================
+ */
+ vector<int>
+countsort ( const vector<int> &weights )
+{
+ vector<int> order(weights.size(), 0);
+ vector<int> counter(n, 0);
+ for (size_t i = 0; i < weights.size(); ++i) if ( weights[i] < n-1 ) ++counter[1+weights[i]];
+ for (int i = 1; i < n; ++i) counter[i] += counter[i-1];
+ for (size_t i = 0; i < weights.size(); ++i) order[counter[weights[i]]++] = i;
+ return order;
+} /* ----- end of function countsort ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: isgodfather
+ * Description:
+ * =====================================================================================
+ */
+ int
+isgodfather ( int godfather, int minsub )
+{
+
+ int sp = 0;
+ stack[sp] = godfather;
+ pos[sp] = 0;
+ weight[godfather] = 1;
+ parent[godfather] = -1;
+
+ int max = 0;
+ while ( sp >= 0 ) {
+ int id = stack[sp];
+ if ( pos[sp] < comm[id].size() && sp > 0 && comm[id][pos[sp]] == stack[sp-1] ) ++pos[sp];
+ if ( pos[sp] < comm[id].size() ) {
+ stack[sp+1] = comm[id][pos[sp]++];
+ pos[++sp] = 0;
+ weight[stack[sp]] = 1;
+ } else {
+ if ( --sp >= 0 ) parent[id] = stack[sp];
+ if ( parent[id] != -1 ) {
+ weight[parent[id]] += weight[id];
+ if ( parent[id] != godfather && weight[parent[id]] > minsub ) return n;
+ }
+ if ( parent[id] == godfather && weight[id] > max ) max = weight[id];
+ }
+ }
+
+ return max;
+} /* ----- end of function isgodfather ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ cin >> n;
+ for (int i = 0; i < n-1; ++i) {
+ int a, b;
+ cin >> a >> b;
+ --a; --b;
+ comm[a].push_back(b);
+ comm[b].push_back(a);
+ }
+
+ vector<int> degree(n, 0);
+ for (int i = 0; i < n; ++i) degree[i] = comm[i].size();
+ vector<int> mafia = countsort(degree);
+
+ vector<int> result;
+ int minsub = n;
+ for (int i = n-1; i >= 0; --i) {
+ int res = isgodfather(mafia[i], minsub);
+ if ( res > minsub ) continue;
+ else if ( res == minsub ) result.push_back(mafia[i]);
+ else { minsub = res; result.clear(); result.push_back(mafia[i]); }
+ }
+
+ vector<int> sorted = countsort(result);
+ cout << (result[sorted[0]] + 1);
+ for (size_t i = 1; i < sorted.size(); ++i)
+ cout << " " << (result[sorted[i]] + 1);
+ cout << endl;
+
+ return 0;
+} /* ---------- end of function main ---------- */
View
@@ -0,0 +1,135 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: godfather.cpp
+ *
+ * Description: POJ 3107
+ *
+ * Version: 1.0
+ * Created: 10/14/2010 05:44:07 PM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Ming Chen (chenming), chenming@sohu-rd.com
+ * Company: imeresearch@sogou
+ *
+ * =====================================================================================
+ */
+
+
+#include <vector>
+#include <iostream>
+
+using namespace std;
+
+const int MAXN = 50000;
+
+class Edge {
+public:
+ int target, weight;
+ Edge(int t, int w) : target(t), weight(w) {};
+};
+
+int n;
+vector<Edge> comm[MAXN];
+int parent[MAXN], stack[MAXN], weight[MAXN];
+size_t pos[MAXN], childpos[MAXN];
+
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: countsort
+ * Description:
+ * =====================================================================================
+ */
+ vector<int>
+countsort ( const vector<int> &weights )
+{
+ vector<int> order(weights.size(), 0);
+ vector<int> counter(n, 0);
+ for (size_t i = 0; i < weights.size(); ++i) if ( weights[i] < n-1 ) ++counter[1+weights[i]];
+ for (int i = 1; i < n; ++i) counter[i] += counter[i-1];
+ for (size_t i = 0; i < weights.size(); ++i) order[counter[weights[i]]++] = i;
+ return order;
+} /* ----- end of function countsort ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: isgodfather
+ * Description:
+ * =====================================================================================
+ */
+ int
+isgodfather ( int godfather, int minsub )
+{
+
+ int sp = 0;
+ stack[sp] = godfather;
+ pos[sp] = 0;
+ weight[godfather] = 1;
+ parent[godfather] = -1;
+
+ int max = 0;
+ while ( sp >= 0 ) {
+ int id = stack[sp];
+ if ( pos[sp] < comm[id].size() && sp > 0 && comm[id][pos[sp]].target == stack[sp-1] ) ++pos[sp];
+ if ( pos[sp] < comm[id].size() ) {
+ if ( comm[id][pos[sp]].weight != 0 ) {
+ if ( id == godfather && comm[id][pos[sp]].weight > max ) max = comm[id][pos[sp]].weight;
+ if ( comm[id][pos[sp]].weight > minsub || (id != godfather && comm[id][pos[sp]].weight == minsub) ) return n;
+ weight[id] += comm[id][pos[sp]++].weight;
+ } else {
+ childpos[sp+1] = pos[sp];
+ stack[sp+1] = comm[id][pos[sp]++].target;
+ pos[++sp] = 0;
+ weight[stack[sp]] = 1;
+ }
+ } else {
+ if ( --sp >= 0 ) parent[id] = stack[sp];
+ if ( parent[id] != -1 ) {
+ weight[parent[id]] += weight[id];
+ comm[parent[id]][childpos[sp+1]].weight = weight[id];
+ if ( parent[id] != godfather && weight[parent[id]] > minsub ) return n;
+ }
+ if ( parent[id] == godfather && weight[id] > max ) max = weight[id];
+ }
+ }
+
+ return max;
+} /* ----- end of function isgodfather ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: main
+ * Description:
+ * =====================================================================================
+ */
+ int
+main ( int argc, char *argv[] )
+{
+ cin >> n;
+ for (int i = 0; i < n-1; ++i) {
+ int a, b;
+ cin >> a >> b;
+ --a; --b;
+ comm[a].push_back(Edge(b, 0));
+ comm[b].push_back(Edge(a, 0));
+ }
+
+ vector<int> result;
+ int minsub = n;
+ for (int i = 0; i < n; ++i) {
+ int res = isgodfather(i, minsub);
+ if ( res > minsub ) continue;
+ else if ( res == minsub ) result.push_back(i);
+ else { minsub = res; result.clear(); result.push_back(i); }
+ }
+
+ vector<int> sorted = countsort(result);
+ cout << (result[sorted[0]] + 1);
+ for (size_t i = 1; i < sorted.size(); ++i)
+ cout << " " << (result[sorted[i]] + 1);
+ cout << endl;
+
+ return 0;
+} /* ---------- end of function main ---------- */
Oops, something went wrong.

0 comments on commit dede3f2

Please sign in to comment.