Advertisement
Guest User

Untitled

a guest
Jul 20th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import java.util.BitSet;
  2. import java.io.IOException;
  3. import java.io.OutputStreamWriter;
  4. import java.io.BufferedWriter;
  5. import java.util.InputMismatchException;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import java.io.Writer;
  9. import java.io.InputStream;
  10.  
  11. /**
  12.  * Built using CHelper plug-in
  13.  * Actual solution is at the top
  14.  *
  15.  * @author ilyakor
  16.  */
  17. public class Main {
  18.     public static void main(String[] args) {
  19.         InputStream inputStream = System.in;
  20.         OutputStream outputStream = System.out;
  21.         InputReader in = new InputReader(inputStream);
  22.         OutputWriter out = new OutputWriter(outputStream);
  23.         Friends solver = new Friends();
  24.         solver.solve(1, in, out);
  25.         out.close();
  26.     }
  27. }
  28.  
  29. class Friends {
  30.     char[][] a;
  31.     int n;
  32.  
  33.     public void solve(int testNumber, InputReader in, OutputWriter out) {
  34.         n = in.nextInt();
  35.         a = new char[n][];
  36.         BitSet[] g = new BitSet[n];
  37.         for (int i = 0; i < n; ++i) {
  38.             a[i] = in.nextToken().toCharArray();
  39.             g[i] = new BitSet(n);
  40.             for (int j = 0; j < n; ++j)
  41.                 if (a[i][j] == '1')
  42.                     g[i].set(j);
  43.         }
  44.         int res = 0;
  45.         for (int i = 0; i < n; ++i)
  46.             for (int j = 0; j < n; ++j) {
  47.                 if (i == j || a[i][j] == '1') continue;
  48.                 BitSet s = (BitSet) g[i].clone();
  49.                 s.and(g[j]);
  50.                 if (s.cardinality() > 0)
  51.                     ++res;
  52.             }
  53.         out.printLine(res);
  54.     }
  55.  
  56. }
  57.  
  58. class InputReader {
  59.     private InputStream stream;
  60.     private byte[] buffer = new byte[10000];
  61.     private int cur;
  62.     private int count;
  63.  
  64.     public InputReader(InputStream stream) {
  65.         this.stream = stream;
  66.     }
  67.  
  68.     public static boolean isSpace(int c) {
  69.         return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  70.     }
  71.  
  72.     public int read() {
  73.         if (count == -1) {
  74.             throw new InputMismatchException();
  75.         }
  76.         try {
  77.             if (cur >= count) {
  78.                 cur = 0;
  79.                 count = stream.read(buffer);
  80.                 if (count <= 0)
  81.                     return -1;
  82.             }
  83.         } catch (IOException e) {
  84.             throw new InputMismatchException();
  85.         }
  86.         return buffer[cur++];
  87.     }
  88.  
  89.     public int readSkipSpace() {
  90.         int c;
  91.         do {
  92.             c = read();
  93.         } while (isSpace(c));
  94.         return c;
  95.     }
  96.  
  97.     public String nextToken() {
  98.         int c = readSkipSpace();
  99.         StringBuilder sb = new StringBuilder();
  100.         while (!isSpace(c)) {
  101.             sb.append((char) c);
  102.             c = read();
  103.         }
  104.         return sb.toString();
  105.     }
  106.  
  107.     public int nextInt() {
  108.         int sgn = 1;
  109.         int c = readSkipSpace();
  110.         if (c == '-') {
  111.             sgn = -1;
  112.             c = read();
  113.         }
  114.         int res = 0;
  115.         do {
  116.             if (c < '0' || c > '9') {
  117.                 throw new InputMismatchException();
  118.             }
  119.             res = res * 10 + c - '0';
  120.             c = read();
  121.         } while (!isSpace(c));
  122.         res *= sgn;
  123.         return res;
  124.     }
  125.  
  126. }
  127.  
  128. class OutputWriter {
  129.     private final PrintWriter writer;
  130.  
  131.     public OutputWriter(OutputStream outputStream) {
  132.         writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  133.     }
  134.  
  135.     public OutputWriter(Writer writer) {
  136.         this.writer = new PrintWriter(writer);
  137.     }
  138.  
  139.     public void print(Object... objects) {
  140.         for (int i = 0; i < objects.length; i++) {
  141.             if (i != 0) {
  142.                 writer.print(' ');
  143.             }
  144.             writer.print(objects[i]);
  145.         }
  146.     }
  147.  
  148.     public void printLine(Object... objects) {
  149.         print(objects);
  150.         writer.println();
  151.     }
  152.  
  153.     public void close() {
  154.         writer.close();
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement