#include <algorithm> #include <iostream> #include <list> #include "sabotaz.h" #include "message.h" using namespace std; class Graph { long V; list<long> *adj; list<long> *adjRepeated; long bridgeUtil(long v, bool visited[], long disc[], long low[], long parent[], long start, long end); public: Graph(long V); void addEdge(long v, long w); long bridge(long start, long end); }; Graph::Graph(long V) { this->V = V; adj = new list<long>[V]; adjRepeated = new list<long>[V]; } void Graph::addEdge(long v, long w) { list<long>::iterator findIter = find(adj[v].begin(), adj[v].end(), w); if(adj[v].end() == findIter) //edge does not already exist { adj[v].push_back(w); adj[w].push_back(v); } else //already exists { adjRepeated[v].push_back(w); adjRepeated[w].push_back(v); } } long Graph::bridgeUtil(long u, bool visited[], long disc[], long low[], long parent[], long start, long end) { static long time = 0; long bridges = 0; visited[u] = true; disc[u] = low[u] = ++time; list<long>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) { long v = *i; if (!visited[v]) { parent[v] = u; bridges += bridgeUtil(v, visited, disc, low, parent, start, end); low[u] = min(low[u], low[v]); if (low[v] > disc[u]){ list<long>::iterator findIter = find(adjRepeated[v].begin(), adjRepeated[v].end(), u); if(findIter == adjRepeated[v].end()){ if(u >= start && v >= start && u<=end && v <=end){ //both in scope, ok bridges++; } else{ //one in scope, count only if smaller is in scope if( u < v){ if(u >= start && u<=end){ bridges++; } } else{ if(v >= start && v<=end){ bridges++; } } } } } } else if (v != parent[u]) low[u] = min(low[u], disc[v]); } return bridges; } long Graph::bridge(long start, long end) { bool *visited = new bool[V]; long *disc = new long[V]; long *low = new long[V]; long *parent = new long[V]; for (long i = 0; i < V; i++) { parent[i] = -1; visited[i] = false; } long bridges = 0; for (long i = 0; i < V; i++){ if (visited[i] == false){ bridges += bridgeUtil(i, visited, disc, low, parent, start, end); } } return bridges; } int main() { Graph g(NumberOfIsles()); long bridges = NumberOfBridges(); for(long i=0; i<bridges; i++){ if(BridgeEndA(i) != BridgeEndB(i)){ g.addEdge(BridgeEndA(i), BridgeEndB(i)); } } if(NumberOfIsles() < NumberOfNodes() || NumberOfNodes() == 1){ if(MyNodeId()==0){ cout<<g.bridge(0, NumberOfIsles()-1)<<endl; } } else{ long oneInstanceScope = (long)(NumberOfIsles()/NumberOfNodes()); long myStartIndex = MyNodeId() * oneInstanceScope; long myEndIndex = myStartIndex + oneInstanceScope - 1; if(MyNodeId() == NumberOfNodes() - 1){ myEndIndex = NumberOfIsles()-1; } long bridges = g.bridge(myStartIndex, myEndIndex); if(MyNodeId()==0){ for(long i=1; i < NumberOfNodes(); i++){ Receive(i); bridges += GetInt(i); } cout<<bridges<<endl; } else{ PutInt(0, bridges); Send(0); } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <algorithm> #include <iostream> #include <list> #include "sabotaz.h" #include "message.h" using namespace std; class Graph { long V; list<long> *adj; list<long> *adjRepeated; long bridgeUtil(long v, bool visited[], long disc[], long low[], long parent[], long start, long end); public: Graph(long V); void addEdge(long v, long w); long bridge(long start, long end); }; Graph::Graph(long V) { this->V = V; adj = new list<long>[V]; adjRepeated = new list<long>[V]; } void Graph::addEdge(long v, long w) { list<long>::iterator findIter = find(adj[v].begin(), adj[v].end(), w); if(adj[v].end() == findIter) //edge does not already exist { adj[v].push_back(w); adj[w].push_back(v); } else //already exists { adjRepeated[v].push_back(w); adjRepeated[w].push_back(v); } } long Graph::bridgeUtil(long u, bool visited[], long disc[], long low[], long parent[], long start, long end) { static long time = 0; long bridges = 0; visited[u] = true; disc[u] = low[u] = ++time; list<long>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) { long v = *i; if (!visited[v]) { parent[v] = u; bridges += bridgeUtil(v, visited, disc, low, parent, start, end); low[u] = min(low[u], low[v]); if (low[v] > disc[u]){ list<long>::iterator findIter = find(adjRepeated[v].begin(), adjRepeated[v].end(), u); if(findIter == adjRepeated[v].end()){ if(u >= start && v >= start && u<=end && v <=end){ //both in scope, ok bridges++; } else{ //one in scope, count only if smaller is in scope if( u < v){ if(u >= start && u<=end){ bridges++; } } else{ if(v >= start && v<=end){ bridges++; } } } } } } else if (v != parent[u]) low[u] = min(low[u], disc[v]); } return bridges; } long Graph::bridge(long start, long end) { bool *visited = new bool[V]; long *disc = new long[V]; long *low = new long[V]; long *parent = new long[V]; for (long i = 0; i < V; i++) { parent[i] = -1; visited[i] = false; } long bridges = 0; for (long i = 0; i < V; i++){ if (visited[i] == false){ bridges += bridgeUtil(i, visited, disc, low, parent, start, end); } } return bridges; } int main() { Graph g(NumberOfIsles()); long bridges = NumberOfBridges(); for(long i=0; i<bridges; i++){ if(BridgeEndA(i) != BridgeEndB(i)){ g.addEdge(BridgeEndA(i), BridgeEndB(i)); } } if(NumberOfIsles() < NumberOfNodes() || NumberOfNodes() == 1){ if(MyNodeId()==0){ cout<<g.bridge(0, NumberOfIsles()-1)<<endl; } } else{ long oneInstanceScope = (long)(NumberOfIsles()/NumberOfNodes()); long myStartIndex = MyNodeId() * oneInstanceScope; long myEndIndex = myStartIndex + oneInstanceScope - 1; if(MyNodeId() == NumberOfNodes() - 1){ myEndIndex = NumberOfIsles()-1; } long bridges = g.bridge(myStartIndex, myEndIndex); if(MyNodeId()==0){ for(long i=1; i < NumberOfNodes(); i++){ Receive(i); bridges += GetInt(i); } cout<<bridges<<endl; } else{ PutInt(0, bridges); Send(0); } } return 0; } |