Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class buildteam{
- static long mod = (long)(pow(10, 9) + 7);
- static int n;
- static int[] team;
- static List<List<Integer>> adj;
- static void fill(int i) {
- Queue<Integer> q = new ArrayDeque<Integer>();;
- q.add(i);
- team[i] = 1;
- while (!q.isEmpty()) {
- int head = q.poll();
- for (int x: adj.get(head)) {
- if (team[x] == 0) {
- team[x] = 3-team[head];
- q.add(x);
- }
- else if (team[x] == team[head]) {
- out.println("IMPOSSIBLE");
- out.close();
- System.exit(0);
- }
- }
- }
- }
- public static void main(String[] args) {
- MyScanner sc = new MyScanner();
- out = new PrintWriter(new BufferedOutputStream(System.out));
- n = sc.nextInt();
- int m = sc.nextInt();
- team = new int[n];
- adj = new ArrayList<List<Integer>>();
- for (int i = 0; i < n; i++) {
- adj.add(new ArrayList<Integer>());
- }
- for (int i = 0; i < m; i++) {
- int a = sc.nextInt()-1;
- int b = sc.nextInt()-1;
- adj.get(a).add(b);
- adj.get(b).add(a);
- }
- for (int i = 0; i < n; i++) {
- if (team[i] == 0) {
- fill(i);
- }
- }
- for (int x: team) out.print(x + " ");
- out.println();
- out.close();
- }
- static long pow(long a, long N) {
- if (N == 0) return 1;
- else if (N == 1) return a;
- else {
- long R = pow(a,N/2);
- if (N % 2 == 0) {
- return R*R;
- }
- else {
- return R*R*a;
- }
- }
- }
- static long powMod(long a, long N) {
- if (N == 0) return 1;
- else if (N == 1) return a % mod;
- else {
- long R = powMod(a,N/2) % mod;
- // out.println(R);
- R = R * R % mod;
- if (N % 2 == 1) {
- R = R* a % mod;
- }
- return R % mod;
- }
- }
- //-----------PrintWriter for faster output---------------------------------
- public static PrintWriter out;
- //-----------MyScanner class for faster input----------
- public static class MyScanner {
- BufferedReader br;
- StringTokenizer st;
- public MyScanner() {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- String next() {
- while (st == null || !st.hasMoreElements()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- String nextLine(){
- String str = "";
- try {
- str = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str;
- }
- }
- //--------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment