Created
August 14, 2019 17:33
-
-
Save DarinMao/0bccbed36e59aed50ff2006a3b85af7f to your computer and use it in GitHub Desktop.
mallcop is a shit problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
import java.lang.*; | |
public class MallCop { | |
private static Graph graph; | |
private static int n; | |
private static int[] distLeaf; | |
private static int[] distStart; | |
private static int ans; | |
public static void main(String[] args) throws FileNotFoundException { | |
for (int i = 1; i <= 20; i++) { | |
String filename = "in/" + i + ".in"; | |
solve(filename); | |
System.out.println(ans); | |
} | |
/* | |
solve("mallcop.in"); | |
for (int i = 0; i < n; i++) { | |
System.out.println(i + ": " + distStart[i] + "," + distLeaf[i]); | |
} | |
System.out.println(ans); | |
*/ | |
} | |
private static void solve(String filename) throws FileNotFoundException { | |
Scanner input = new Scanner(new File(filename)); | |
n = input.nextInt(); | |
int k = input.nextInt() - 1; | |
graph = new Graph(n); | |
distLeaf = new int[n]; | |
distStart = new int[n]; | |
for (int i = 1; i < n; i++) { | |
int s = input.nextInt(); | |
int d = input.nextInt(); | |
s--; | |
d--; | |
int w = input.nextInt(); | |
// int w = 1; | |
graph.addEdge(s, d, w); | |
} | |
start(k); | |
leaf(); | |
ans = 0; | |
dfs(k, -1); | |
} | |
private static void dfs(int i, int p) { | |
if (p != -1 && distLeaf[i] <= distStart[i] && distLeaf[p] > distStart[p]) { | |
ans++; | |
} | |
for (int j = 0; j < graph.adjacencylist[i].size(); j++) { | |
if (p != graph.adjacencylist[i].get(j).destination) { | |
dfs(graph.adjacencylist[i].get(j).destination, i); | |
} | |
} | |
} | |
private static void start(int i) { | |
boolean[] d = new boolean[n]; | |
Deque<Integer> s = new ArrayDeque<Integer>(); | |
s.push(i); | |
d[i] = true; | |
distStart[i] = 0; | |
while (!s.isEmpty()) { | |
int j = s.pop(); | |
boolean leaf = true; | |
for (int k = 0; k < graph.adjacencylist[j].size(); k++) { | |
Edge e = graph.adjacencylist[j].get(k); | |
if (!d[e.destination]) { | |
d[e.destination] = true; | |
distStart[e.destination] = distStart[j] + e.weight; | |
s.push(e.destination); | |
leaf = false; | |
} | |
} | |
if (leaf) { | |
distLeaf[j] = 0; | |
} else { | |
distLeaf[j] = n+1; | |
} | |
} | |
} | |
private static void leaf() { | |
boolean[] d = new boolean[n]; | |
Queue<Integer> q = new ArrayDeque<Integer>(); | |
for (int i = 0; i < distLeaf.length; i++) { | |
if (distLeaf[i] == 0) { | |
q.add(i); | |
d[i] = true; | |
} | |
} | |
while (!q.isEmpty()) { | |
int i = q.remove(); | |
for (int j = 0; j < graph.adjacencylist[i].size(); j++) { | |
Edge e = graph.adjacencylist[i].get(j); | |
if (!d[e.destination]) { | |
d[e.destination] = true; | |
distLeaf[e.destination] = distLeaf[i] + e.weight; | |
q.add(e.destination); | |
} | |
} | |
} | |
} | |
static class Edge { | |
int source; | |
int destination; | |
int weight; | |
public Edge(int source, int destination, int weight) { | |
this.source = source; | |
this.destination = destination; | |
this.weight = weight; | |
} | |
} | |
static class Graph { | |
int vertices; | |
LinkedList<Edge> [] adjacencylist; | |
Graph(int vertices) { | |
this.vertices = vertices; | |
adjacencylist = new LinkedList[vertices]; | |
for (int i = 0; i < vertices ; i++) { | |
adjacencylist[i] = new LinkedList<>(); | |
} | |
} | |
public void addEdge(int source, int destination, int weight) { | |
Edge edge = new Edge(source, destination, weight); | |
adjacencylist[source].add(edge); | |
Edge edge2 = new Edge(destination, source, weight); | |
adjacencylist[destination].add(edge2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment