UVa10596 - Morning Walk(并查集)

简介: UVa10596 - Morning Walk(并查集)
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.io.BufferedReader;
importjava.io.PrintWriter;
importjava.io.OutputStreamWriter;
importjava.io.StreamTokenizer;
classMain{
publicstaticfinalbooleanDEBUG=false;
publicstaticintN=30;
publicBufferedReadercin;
publicPrintWritercout;
publicStreamTokenizertokenizer;
publicintn, r;
publicint[] p, d;
publicvoidinit()
    {
try {
if (DEBUG) {
cin=newBufferedReader(newInputStreamReader(
newFileInputStream("d:\\OJ\\uva_in.txt")));
            } else {
cin=newBufferedReader(newInputStreamReader(System.in));
            }
cout=newPrintWriter(newOutputStreamWriter(System.out));
tokenizer=newStreamTokenizer(cin);
        } catch (Exceptione) {
e.printStackTrace();
        }
    }
publicStringnext()
    {
try {
tokenizer.nextToken();
if (tokenizer.ttype==StreamTokenizer.TT_EOF)
returnnull;
elseif (tokenizer.ttype==StreamTokenizer.TT_NUMBER) {
returnString.valueOf((int) tokenizer.nval);
            }
returnnull;
        } catch (Exceptione) {
e.printStackTrace();
returnnull;
        }
    }
publicintfind(intx)
    {
returnp[x] ==x?x : (p[x] =find(p[x]));
    }
publicbooleaninput()
    {
Strings=next();
if (s==null) returnfalse;
n=Integer.parseInt(s);
s=next();
r=Integer.parseInt(s);
p=newint[n];
d=newint[n];
for (inti=0; i<n; i++) {
p[i] =i;
d[i] =0;
        }
for (inti=0; i<r; i++) {
s=next();
inta=Integer.parseInt(s);
s=next();
intb=Integer.parseInt(s);
p[find(b)] =find(a);
d[a]++;
d[b]++;
        }
returntrue;
    }
publicvoidsolve()
    {
booleanok=true;
if (n==0||r<=1) ok=false;
introot=find(0);
for (inti=0; i<n&&ok; i++) {
if (d[i] !=0) {
if (find(i) !=root||d[i] %2!=0) {
ok=false;
break;
                }
            }
        }
if (ok) {
cout.println("Possible");
        } else {
cout.println("Not Possible");
        }
cout.flush();
    }
publicstaticvoidmain(String[] args)
    {
Mainsolver=newMain();
solver.init();
while (solver.input()) {
solver.solve();
        }
    }
}
目录
相关文章
|
C++
poj 2182 Lost Cows(树状数组)
FJ有n头牛,现在它们排成一排,每一头牛都有一个不同的编号(1-n),现在知道从第二头牛开始每头牛左边比自己编号小的牛的数目,让你确定每头牛的编号。
43 0
UVa11420 - Chest of Drawers(动态规划)
UVa11420 - Chest of Drawers(动态规划)
49 0
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
59 0
|
机器学习/深度学习 测试技术
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
UVA122 树的层次遍历 Trees on the level
UVA122 树的层次遍历 Trees on the level
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)
【1094】The Largest Generation (树DFS-水题)
基础题。 这类题也是直接DFS,不是像上次的【1079】&【1090】供应树DFS一样是到了叶子结点就进行“更新/结算”类型,这题直接利用DFS统计每层的结点个数即可。
100 0