隨筆-16  評論-0  文章-0  trackbacks-0

          ?????? 該程序用一個數組id,其中每項對應一個對象,讀取小于N個非負整數對序列(‘p q’,意思為對象p與對象q連通),并且打印在給出的連通對基礎上還沒有連通的對。定義如下屬性:當且僅當p與q連通時,id[p]=id[q]。

          ?????? 例如輸入3 4,由于先前沒有任何連通對,所以打印3 4,再輸入 4 9 ,先前給出的連通對是3 4,在此基礎上得不到4 9連通(即把4 9識為提供了一個新的連通),所以同樣打印4 9,當再輸入3 9的時候,由于前面已經給出了3 4及4 9,由可傳遞性可以得到3 9連通,即3 9不再是一個新的連通,故不打印。
          ?
          public class QuickF
          ? {
          ??? public static void main(String[] args)
          ??? {
          ????? int N = Integer.parseInt(args[0]);
          ????? int id[] = new int[N];
          ????? for (int i = 0; i < N ; i++) id[i] = i;
          ????? for( In.init(); !In.empty(); )
          ??????? {
          ????????? int p = In.getInt(), q = In.getInt();
          ????????? int t = id[p];
          ????????? if (t == id[q]) continue;
          ????????? for (int i = 0;i<N;i++)
          ??????????? if (id[i] == t) id[i] = id[q];
          ????????? Out.println(p+" "+q+" ");
          ??????? }
          ??? }
          ? }??
          ?
          ? public class Out
          ? {
          ??? public static void print(String s)
          ???? {
          ?System.out.print(s);
          ???? }
          ??? public static void println(String s)
          ???? {
          ??????? System.out.println(s);
          ???? }
          ? }

          ? import java.io.*;
          ? public class In
          ? {
          ??? private static int c;

          ??? private static boolean blank()
          ????? {
          ??????? return Character.isWhitespace((char) c);
          ????? }
          ??? private static void readC()
          ????? {
          ??????? try
          ????????? {
          ??????????? c = System.in.read();
          ????????? }
          ??????? catch (IOException e)
          ????????? {
          ??????????? c = -1;
          ????????? }
          ????? }
          ??? public static void init()
          ????? {
          ??????? readC();
          ????? }
          ??? public static boolean empty()
          ????? {
          ??????? return c == -1;
          ????? }
          ??? public static String getString()
          ????? {
          ??????? if (empty()) return null;
          ??????? String s = "";
          ??????? do
          ????????? {
          ??????????? s += (char) c; readC();
          ????????? }
          ??????? while (!(empty() | blank()));
          ??????? while (!empty() && blank()) readC();
          ??????? return s;
          ????? }
          ??? public static int getInt()
          ????? {
          ??????? return Integer.parseInt(getString());
          ????? }
          ??? public static double getDouble()
          ????? {
          ??????? return Double.parseDouble(getString());
          ????? }
          ?? }

          ?

          posted on 2006-07-29 11:42 尨奇 閱讀(188) 評論(0)  編輯  收藏 所屬分類: algorithms in java
          主站蜘蛛池模板: 乃东县| 岳西县| 杂多县| 盐山县| 祁门县| 泰安市| 阳山县| 德阳市| 蓝山县| 武城县| 盱眙县| 海淀区| 砚山县| 永定县| 黑龙江省| 屯昌县| 齐齐哈尔市| 梁山县| 太保市| 资兴市| 柞水县| 舒城县| 丽江市| 浦东新区| 理塘县| 景谷| 江阴市| 汕尾市| 富宁县| 南木林县| 新沂市| 平潭县| 吉水县| 兖州市| 察雅县| 广南县| 理塘县| 都昌县| 肇源县| 绥德县| 瑞丽市|