Editing
2015 Jan Gold Problem 3 Grass Cownoisseur
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Java === <pre> import java.util.*; public class Main { static final int MAX_NODES = 100010; static int sccNodeIdx, sccLabel, sccStackPtr; static int[] sccIndices = new int[MAX_NODES]; static int[] sccLinks = new int[MAX_NODES]; static int[] sccStack = new int[MAX_NODES]; static int[][] memo = new int[2][MAX_NODES]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numNodes = sc.nextInt(); int numEdges = sc.nextInt(); ArrayList<ArrayList<Integer>> adjList = new ArrayList<>(); for(int i = 0; i < numNodes; i++) adjList.add(new ArrayList<>()); for(int i = 0; i < numEdges; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; adjList.get(u).add(v); } ArrayList<Integer> labels = computeSCC(adjList); adjList = collapseGraph(adjList, labels); int startLabel = labels.get(0); ArrayList<ArrayList<Integer>> reverseAdjList = new ArrayList<>(); for(int i = 0; i < adjList.size(); i++) reverseAdjList.add(new ArrayList<>()); for(int node = 0; node < adjList.size(); node++) for(int edgeNode : adjList.get(node)) reverseAdjList.get(edgeNode).add(node); ArrayList<Integer> weights = new ArrayList<>(Collections.nCopies(adjList.size(), 0)); for(int node = 0; node < numNodes; node++) weights.set(labels.get(node), weights.get(labels.get(node)) + 1); int maxWeight = weights.get(startLabel); for(int[] row : memo) Arrays.fill(row, -1); for(int node = 0; node < adjList.size(); node++) { int path1 = computeMaxWeightPath(0, weights, adjList, node, startLabel); if(path1 < 0) continue; for(int nextNode : adjList.get(node)) { int path2 = computeMaxWeightPath(1, weights, reverseAdjList, nextNode, startLabel); if(path2 >= 0) maxWeight = Math.max(maxWeight, weights.get(startLabel) + path1 + path2); } } System.out.println(maxWeight); } static ArrayList<ArrayList<Integer>> collapseGraph(ArrayList<ArrayList<Integer>> adjList, ArrayList<Integer> labels) { int numNodes = adjList.size(); ArrayList<ArrayList<Integer>> resultGraph = new ArrayList<>(); for(int i = 0; i < Collections.max(labels) + 1; i++) resultGraph.add(new ArrayList<>()); for(int node = 0; node < numNodes; node++) { int labelX = labels.get(node); for(int edgeNode : adjList.get(node)) { int labelY = labels.get(edgeNode); if(labelX != labelY) resultGraph.get(labelX).add(labelY); } } for(ArrayList<Integer> v : resultGraph) { Collections.sort(v); v = new ArrayList<>(new LinkedHashSet<>(v)); } return resultGraph; } static void tarjanDFS(int node, ArrayList<ArrayList<Integer>> adjList, ArrayList<Integer> labels) { int index = sccIndices[node] = sccNodeIdx++; sccLinks[node] = index; sccStack[sccStackPtr++] = node; for(int edgeNode : adjList.get(node)) { if(sccIndices[edgeNode] == -1) { tarjanDFS(edgeNode, adjList, labels); sccLinks[node] = Math.min(sccLinks[node], sccLinks[edgeNode]); } else { sccLinks[node] = Math.min(sccLinks[node], sccIndices[edgeNode]); } } if(index == sccLinks[node]) { int lastNode; do { lastNode = sccStack[--sccStackPtr]; sccIndices[lastNode] = adjList.size(); labels.set(lastNode, sccLabel); } while(lastNode != node); sccLabel++; } } static ArrayList<Integer> computeSCC(ArrayList<ArrayList<Integer>> adjList) { int numNodes = adjList.size(); ArrayList<Integer> labels = new ArrayList<>(Collections.nCopies(numNodes, -1)); Arrays.fill(sccIndices, -1); sccNodeIdx = sccLabel = sccStackPtr = 0; for(int node = 0; node < numNodes; node++) if(labels.get(node) == -1) tarjanDFS(node, adjList, labels); return labels; } static int computeMaxWeightPath(int cid, ArrayList<Integer> weights, ArrayList<ArrayList<Integer>> adjList, int startNode, int targetNode) { if(startNode == targetNode) return 0; if(memo[cid][startNode] != -1) return memo[cid][startNode]; memo[cid][startNode] = -2; for(int edgeNode : adjList.get(startNode)) { int result = computeMaxWeightPath(cid, weights, adjList, edgeNode, targetNode); if(result >= 0) memo[cid][startNode] = Math.max(memo[cid][startNode], weights.get(startNode) + result); } return memo[cid][startNode]; } } </pre>
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information