import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GraphReverse {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader in = new FileReader("tinyDG.txt");
BufferedReader br = new BufferedReader(in);
int tempchar;
String str = "";
str = br.readLine();
//System.out.println(str);
int V = Integer.parseInt(str.trim());
str = br.readLine();
//System.out.println(str.trim());
int E = Integer.parseInt(str);
int[][] g = new int[V][V];
str = "";
int a = 0, b = 0;
while ((tempchar = br.read()) != -1) {
str = str + (char) tempchar;
if ((char) tempchar == ' ') {
if(!str.equals(""))
a = Integer.parseInt(str.trim());
str = "";
}
if ((char) tempchar == '\n') {
b = Integer.parseInt(str.trim());
g[a][b] = 1;
//g[b][a] = 1;
str = "";
}
}
br.close();
in.close();
str = "";
for (int i = 0; i < V; i++) {
System.out.print("\n\r"+i+": ");
for (int j = 0; j < V; j++) {
if(g[i][j]==1){
System.out.print(j+" ");
}
}
}
System.out.println("\n\r\n\r反向图:");
int[][] h=new int[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if(g[i][j]==1){
h[j][i]=1;
}
}
}
for (int i = 0; i < V; i++) {
System.out.print("\n\r"+i+": ");
for (int j = 0; j < V; j++) {
if(h[i][j]==1){
System.out.print(j+" ");
}
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
