AcWing 837. 连通块中点的数量 - JAVA
原题链接
简单
作者:
acw_weian
,
2020-04-21 21:53:50
,
所有人可见
,
阅读 1217
import java.io.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception{
String[] s = read.readLine().split(" ");
int n = Integer.valueOf(s[0]);
int t = Integer.valueOf(s[1]);
UFind uf = new UFind(n);
for(int i = 0; i < t; i++){
s = read.readLine().split(" ");
if("C".equals(s[0])){
uf.union(Integer.valueOf(s[1]), Integer.valueOf(s[2]));
}else if("Q1".equals(s[0])){
int f1 = uf.find(Integer.valueOf(s[1]));
int f2 = uf.find(Integer.valueOf(s[2]));
String out = f1 == f2 ? "Yes" : "No";
log.write(out + "\n");
}else{
int f = uf.find(Integer.valueOf(s[1]));
log.write(uf.size[f] + "\n");
}
}
log.flush();
}
static class UFind{
static int[] size;
static int[] father;
UFind(int n){
size = new int[n + 1];
father = new int[n + 1];
for(int i = 1; i <= n; i++){
father[i] = i;
size[i] = 1;
}
}
public static int find(int n){
if(father[n] == n) return n;
int f = find(father[n]);
father[n] = f;
return f;
}
public static void union(int n1, int n2){
int f1 = find(n1);
int f2 = find(n2);
if(f1 == f2) return;
father[f1] = f2;
size[f2] += size[f1];
}
}
}
你好,bufferedwriter的write明明传入的应该是String,如果传入int型就会打印出乱码,为什么你这个里边传入的是int也能正常打印呢?
哦哦,我明白了,应该是后面加了字符串的原因
哦哦,我明白了,应该是后面加了字符串的原因