关注
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()]++;
}
}
}
}
查看原帖
点赞 评论
相关推荐
07-25 15:10
门头沟学院 嵌入式软件开发 点赞 评论 收藏
分享
点赞 评论 收藏
分享

点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 哪些公司开提前批了? #
28965次浏览 271人参与
# 工作中哪个瞬间让你想离职 #
28297次浏览 194人参与
# 在职场上,你最讨厌什么样的同事 #
16184次浏览 159人参与
# 机械人,秋招第一次笔试的企业是哪家? #
41040次浏览 325人参与
# 哪些公司校招卡第一学历 #
72587次浏览 290人参与
# 华子oc时间线 #
1244862次浏览 6487人参与
# 风评不好的公司,你会去吗? #
65231次浏览 459人参与
# Offer比较,你最看重什么? #
192030次浏览 1308人参与
# 担心入职之后被发现很菜怎么办 #
138878次浏览 807人参与
# 实习如何「偷」产出? #
54551次浏览 1381人参与
# 入职以后才知道的校招谎言 #
88897次浏览 584人参与
# 小米硬件提前批进度交流 #
171043次浏览 1526人参与
# 不卡学历的大厂有哪些? #
31668次浏览 241人参与
# 两会劳动法放大招 #
76652次浏览 692人参与
# 校招阶段,学历VS技术哪个更重要? #
18607次浏览 189人参与
# 机械人春招想让哪家公司来捞你? #
349505次浏览 3088人参与
# 除了主业以外,你还有哪些其他收入? #
13012次浏览 202人参与
# 工作丧失热情的瞬间 #
294307次浏览 2373人参与
# 你最满意的offer薪资是哪家公司? #
33190次浏览 177人参与
# 腾讯音乐求职进展汇总 #
98363次浏览 571人参与