关注
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a;
int b;
Map<Integer,Integer> map1= new HashMap<>();//度
Map<Integer, Integer> map2 = new HashMap<>();//边
for(int i=1;i<=n;i++) {
map1.put(i, 0);
}
for (int i = 0; i < n-1; i++) {
a = sc.nextInt();
int c1=map1.get(a);
map1.put(a, ++c1);
b=sc.nextInt();
map2.put(b, a);
}
System.out.println(map1);
int[] count = new int[n+1];
while (!map1.isEmpty()) {
Set<Map.Entry<Integer, Integer>> set = map1.entrySet();
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(set);
con(list, count, map2, map1);
}
for(int i=1;i<count.length;i++) {
if (i == count.length) {
System.out.print(count[i]);
}
else{
System.out.print(count[i]+" ");
}
}
}
public static void con(List<Map.Entry<Integer, Integer>> list,int [] count,Map<Integer,Integer> map2,Map<Integer,Integer> map1) {
for (Map.Entry entry : list) {
if((Integer)entry.getValue()==0){
count[(Integer)entry.getKey()]++;
map1.remove(entry.getKey());
if(map2.containsKey(entry.getKey())){
Integer edge=map2.get(entry.getKey());
int a = map1.get(edge);
if(a>0)
map1.put(edge, --a);
}
} else if ((Integer) entry.getValue() > 0) {
count[(Integer)entry.getKey()]++;
}
}
}
}
查看原帖
点赞 评论
相关推荐
Java抽象小篮子:大一真别急,再玩一年吧,找后端开发的话直接把第三个项目删了,建立怎么写可以看看我之前发的帖子
点赞 评论 收藏
分享
06-26 16:46
广东海洋大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 哪些公司开提前批了? #
25809次浏览 259人参与
# 华子oc时间线 #
1244101次浏览 6483人参与
# 风评不好的公司,你会去吗? #
60478次浏览 435人参与
# 实习如何「偷」产出? #
50211次浏览 1316人参与
# 除了主业以外,你还有哪些其他收入? #
11937次浏览 201人参与
# 不卡学历的大厂有哪些? #
28580次浏览 224人参与
# 校招阶段,学历VS技术哪个更重要? #
17347次浏览 186人参与
# 职场新人体验 #
25125次浏览 234人参与
# 哪些公司校招卡第一学历 #
67534次浏览 266人参与
# 腾讯音乐求职进展汇总 #
98094次浏览 570人参与
# 社恐入职新公司如何融入团队 #
11856次浏览 63人参与
# 校园里的破防时刻 #
11142次浏览 125人参与
# Offer比较,你最看重什么? #
191588次浏览 1301人参与
# 你投递的公司有几家约面了? #
108911次浏览 779人参与
# 你觉得技术面多长时间合理? #
100236次浏览 720人参与
# 你最满意的offer薪资是哪家公司? #
33088次浏览 176人参与
# 你今年的平均薪资是多少? #
134016次浏览 686人参与
# 你的秋招第一场笔试是哪家 #
147616次浏览 1484人参与
# 正在实习的碎碎念 #
1454701次浏览 13469人参与
# 实习打杂,要跑路吗 #
18077次浏览 202人参与