Guest User

Untitled

a guest
Jul 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.StringTokenizer;
  8. import java.util.ArrayList;
  9.  
  10. public class eMain implements Runnable {
  11. StringTokenizer st;
  12. BufferedReader in;
  13. PrintWriter out;
  14.  
  15. ArrayList<Integer>[] graph;
  16. ArrayList<Integer> answ;
  17.  
  18.  
  19. int n,m;
  20. int num;
  21. int time=0;
  22. int pr[];
  23. int counter=0;
  24. int anscol[];
  25. int aa[];
  26. int bb[];
  27. int enter[];
  28. int colors[];
  29. int leave[];
  30. int low[];
  31. int art[];
  32. int nums[];
  33. public static void main(String[] args) {
  34. new Thread(new eMain()).run();
  35. }
  36.  
  37. public void run() {
  38. try {
  39. in = new BufferedReader(new FileReader("biconv.in"));
  40. out = new PrintWriter(new FileWriter("biconv.out"));
  41. solve();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. out.flush();
  46. out.close();
  47. }
  48. }
  49.  
  50. String nextToken() throws IOException {
  51. while (st == null || !st.hasMoreTokens()) {
  52. st = new StringTokenizer(in.readLine());
  53. }
  54. return st.nextToken();
  55. }
  56.  
  57. int nextInt() throws NumberFormatException, IOException {
  58. return Integer.parseInt(nextToken());
  59. }
  60.  
  61. long nextLong() throws NumberFormatException, IOException {
  62. return Long.parseLong(nextToken());
  63. }
  64.  
  65. double nextDouble() throws NumberFormatException, IOException {
  66. return Double.parseDouble(nextToken());
  67. }
  68.  
  69. int min (int a, int b) {
  70. if (a<=b) {
  71. return a;
  72. } else {
  73. return b;
  74. }
  75. }
  76. void DFSVISIT(int a) {
  77.  
  78. int kids = 0;
  79. colors[a] = 1;
  80. low[a]=enter[a]=++time;
  81.  
  82. int s=graph[a].size();
  83. for (int i=0; i<s; i++){
  84. int ii=aa[graph[a].get(i)];
  85. if(colors[ii]==0) {
  86. pr[ii]=a;
  87. DFSVISIT(ii);
  88. low[a]=min(low[a],low[ii]);
  89.  
  90. if ((pr[a]!=-1)&&(low[ii]>=enter[a])) {
  91. art[a]=1;
  92. }
  93. kids++;
  94. } else if((colors[ii]== 1) && (ii!=pr[a])) {
  95. low[a] = min(low[a],enter[ii]);
  96. }
  97.  
  98. }
  99. if (pr[a]==-1){
  100. if (kids>=2) {
  101. art[a]=1;
  102.  
  103. } else {
  104. art[a]=0;
  105. }
  106. }
  107. colors[a]=2;
  108. leave[a]=++time;
  109.  
  110.  
  111. }
  112. void DFS() {
  113. for (int i=0; i<n; i++) {
  114.  
  115. if (colors[i]==0){
  116.  
  117. DFSVISIT(i);
  118. }
  119. }
  120. }
  121. void DFSVISIT2(int a,int b){
  122. colors[a]=1;
  123.  
  124. int s=graph[a].size();
  125.  
  126. for (int i=0; i<s; i++){
  127. int ii=aa[graph[a].get(i)];
  128. if (colors[ii]==1){
  129. anscol[graph[a].get(i)%m]=b;
  130. anscol[graph[a].get(i)]=b;
  131. if (art[ii]==1) {
  132. counter++;
  133. }
  134. }
  135.  
  136.  
  137. }
  138. for (int i=0; i<s; i++){
  139. int ii=aa[graph[a].get(i)];
  140.  
  141. if (colors[ii]==0){
  142. anscol[graph[a].get(i)%m]=counter;
  143. anscol[graph[a].get(i)]=counter;
  144. DFSVISIT2(ii,counter);
  145.  
  146. }
  147.  
  148.  
  149. }
  150. colors[a]=2;
  151.  
  152.  
  153. }
  154.  
  155. @SuppressWarnings("unchecked")
  156. void solve() throws NumberFormatException, IOException {
  157.  
  158.  
  159.  
  160. n=nextInt();
  161. m=nextInt();
  162. answ = new ArrayList();
  163.  
  164. colors=new int[n];
  165. pr=new int[n];
  166. low=new int[n];
  167. enter=new int[n];
  168. leave=new int[n];
  169. art=new int[n];
  170. nums=new int[n];
  171. anscol=new int[2*m];
  172. aa=new int[2*m];
  173. bb=new int[2*m];
  174. for (int i=0; i<n; i++) {
  175. pr[i]=-1;
  176. art[i]=0;
  177. }
  178. graph=new ArrayList[n];
  179. for(int i = 0; i < n; i++) {
  180. graph[i] = new ArrayList<Integer>();
  181.  
  182. }
  183. for (int i=0; i<m; i++) {
  184. int a, b;
  185. a=nextInt();
  186. b=nextInt();
  187. graph[a-1].add(i);
  188. graph[b-1].add(i+m);
  189. aa[i]=b-1;
  190. aa[i+m]=a-1;
  191. }
  192. DFS();
  193. for (int i=0; i<n;i++) {
  194. colors[i]=0;
  195. }
  196. for (int i=0; i<n;i++) {
  197. counter++;
  198. if (colors[i]==0) {
  199. DFSVISIT2(i,1);
  200. }
  201. }
  202. for (int i=0; i<n; i++){
  203. if (art[i]==1) {
  204. out.print(i+1+" ");
  205. }
  206. }
  207. out.println("");
  208. for (int i=0; i<m; i++){
  209. out.print(anscol[i]+" ");
  210. }
  211. }
  212. }
Add Comment
Please, Sign In to add comment