importjava.io.*;importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args)throwsIOException{BufferedReader br =newBufferedReader(newInputStreamReader(System.in));intT=Integer.parseInt(br.readLine().trim());while(T-->0){int n =Integer.parseInt(br.readLine().trim());int m =100000;DisjointSetUnion dis =newDisjointSetUnion(m);for(int i =0; i < n;++i){String[] str = br.readLine().trim().split(" ");int x =Integer.parseInt(str[0]);int y =Integer.parseInt(str[1]);
dis.unionSet(x-1,y-1);}int max =0;for(int k =0; k < m;++k){
max =Math.max(max,dis.nodeNum[k]);}System.out.println(max);}}}//并查集(管理一系列不想交的集合),具有查询和合并的功能classDisjointSetUnion{int[] f;//存储每个元素的根节点int[] nodeNum;//记录当前结点为根节点的结点数量int[] rank;//记录每个根节点对应树的深度int n;//元素个数publicDisjointSetUnion(int n){this.n = n;this.rank =newint[n];//记录每个根节点对应树的深度this.f =newint[n];//存储每个元素的父节点this.nodeNum =newint[n];for(int i =0; i < n; i++){this.rank[i]=1;//初始值设为1this.f[i]= i;//一开始将父节点设为自己this.nodeNum[i]=1;}}//压缩路径查询方法,递归实现//一层一层访问父节点,直至根节点(根节点的标志就是父节点是本身)。要判断两个元素是否属于同一个集合,只需要看它们的根节点是否相同即可publicintfind(int x){return f[x]== x ? x :(f[x]=find(f[x]));}//按秩合并方法publicbooleanunionSet(int x,int y){int fx =find(x), fy =find(y);//找到两个结点的根节点if(fx == fy){//两个结点的根节点相同不用合并returnfalse;}if(rank[fx]< rank[fy]){//以fx为根节点的树的深度小于以fy为根节点的树的深度
f[fx]= fy;
nodeNum[fy]+= nodeNum[fx];}elseif(rank[fx]> rank[fy]){
f[fy]= fx;
nodeNum[fx]+= nodeNum[fy];}else{
f[fx]= fy;
rank[fy]+=1;
nodeNum[fy]+= nodeNum[fx];}returntrue;}}
第二题
importjava.io.*;importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args)throwsIOException{BufferedReader br =newBufferedReader(newInputStreamReader(System.in));String s = br.readLine().trim();int k =Integer.parseInt(br.readLine().trim());Set<String> set =newHashSet<>();PriorityQueue<String> pq =newPriorityQueue<>((o1, o2)-> o2.compareTo(o1));for(int len =1; len <= k;++len){for(int i =0; i < s.length()- len +1;++i){String substr = s.substring(i, i + len);if(set.add(substr)){if(pq.size()< k){
pq.offer(substr);}else{if(pq.peek().compareTo(substr)>0){
pq.poll();
pq.offer(substr);}}}}}System.out.println(pq.peek());}}