Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by santanu ( 7 years ago )
package contests;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Solution {
static TreeMap<Integer, List<Integer>> adjList;
static Set<Integer> visited;
static Stack<Integer> stack;
public static void main(String[] args) throws IOException {
Reader in = new Reader();
stack = new Stack<>();
int N = in.ni(), M = in.ni();
Graph g = new Graph(N);
for(int i=0;i<M;i++) {
int v1 = in.ni()-1, v2 = in.ni()-1;
g.addEdge(v1, v2);
}
for(int v: g.getAllVertices()) {
if(visited.contains(v))continue;
explore(v, g);
}
while(!stack.empty())
System.out.printf("%d ", stack.pop()+1);
}
private static void explore(int v, Graph g) {
visited.add(v);
List<Integer> l = new ArrayList<>();
for(int kk : g.getAdjVertices(v)) {
l.add(kk);
if(visited.contains(kk))continue;
explore(kk, g);
}
Collections.sort(l, Collections.reverseOrder());
stack.addAll(l);
}
static class Graph {
Graph(int n) {
adjList = new TreeMap<>();
visited = new HashSet<>();
for(int i=0;i<n;i++)
adjList.putIfAbsent(i, new ArrayList<>());
}
void addEdge(int i, int j) {
List<Integer> l = adjList.get(i);
l.add(j);
adjList.put(i, l);
}
List<Integer> getAdjVertices(int k) {
return adjList.get(k);
}
Set<Integer> getAllVertices() {
return adjList.keySet();
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String rl() throws IOException
{
byte[] buf = new byte[1 << 16]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int ni() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nl() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nd() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
Revise this Paste