我的代码:
import java.util.*;
public class Main {
user xmFC = new user(1);
ArrayList<user> fc = xmFC.friendList;
public static void main(String[] args) throws Exception{
Main friendCircle = new Main();
Scanner cin = new Scanner(System.in);
int m = cin.nextInt();
if(m>=1000){System.exit(0);}
for(int i=2;i<=m;i++){
friendCircle.addFriend(i);
}
int n = cin.nextInt();
for(int i=0;i<n;i++){
int a = cin.nextInt();
if(a>m){System.exit(0);}
int b = cin.nextInt();
if(b>m){System.exit(0);}
friendCircle.isFriend(a, b);
}
int w = cin.nextInt();
for(int i=0;i<w;i++){
int u = cin.nextInt();
if(u>m){System.exit(0);}
int uc = cin.nextInt();
if(uc>=1000){System.exit(0);}
(u, uc);
}
int rId = cin.nextInt();
if(rId ==1){
friendCircle.xmFC.readComment();
}else{
friendCircle.read(rId);}
cin.close();
}
public void addFriend(int userId){
user friend = new user(userId);
xmFC.beFriend(friend);
friend.beFriend(xmFC);
}
public void isFriend(int a,int b){
fc.get(a-1).beFriend(fc.get(b-1));
fc.get(b-1).beFriend(fc.get(a-1));
}
public void comment(int u,int uc){
fc.get(u-1).comment(uc);
}
public void read(int rId){
fc.get(rId-1).readComment();
}
}
class user {
int id ;
ArrayList<Integer> cIds = new ArrayList<Integer>();
boolean hasComment;
ArrayList<user> friendList = new ArrayList<user>();
public user(int userId) {
this.id = userId;
friendList.add(this);
}
public void beFriend(user b){
if(!this.friendList.contains(b)){
friendList.add(b);
}
}
public void comment(int cId){
this.cIds.add(cId);
this.hasComment = true;
}
public void readComment() {
for(int i=0;i<friendList.size();i++){
user readUF =
friendList.get(i);
if(readUF.hasComment){
int fid = readUF.id;
for(int j=0;j<readUF.cIds.size();j++){
int fcId = readUF.cIds.get(j);
System.out.print(String.format("%1$8d", fid));
System.out.println(String.format("%1$8d", fcId));
}
}
}
}
}