小A参加了一个n人的活动,每个人都有一个唯一编号i(i>=0 & i<n),其中m对相互认识,在活动中两个人可以通过互相都认识的一个人介绍认识。现在问活动结束后,小A最多会认识多少人?
小A参加了一个n人的活动,每个人都有一个唯一编号i(i>=0 & i<n),其中m对相互认识,在活动中两个人可以通过互相都认识的一个人介绍认识。现在问活动结束后,小A最多会认识多少人?
第一行聚会的人数:n(n>=3 & n<10000);
第二行小A的编号: ai(ai >= 0 & ai < n);
第三互相认识的数目: m(m>=1 & m
< n(n-1)/2);
第4到m+3行为互相认识的对,以','分割的编号。
输出小A最多会新认识的多少人?
7 5 6 1,0 3,1 4,1 5,3 6,1 6,5
3
import java.util.*; // BFS: 认识是双向的, 从A点出发BFS (初始queue、set要注意) public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int ai = in.nextInt(); List<List<Integer>> list = new ArrayList<>(); // 邻接表 for (int i = 0; i < n; i++) list.add(new ArrayList<>()); int m = in.nextInt(); while (m-- > 0) { String[] sp = in.next().split(","); int u = Integer.parseInt(sp[0]); int v = Integer.parseInt(sp[1]); list.get(u).add(v); list.get(v).add(u); } Queue<Integer> queue = new LinkedList<>(); queue.offer(ai); for (int v : list.get(ai)) queue.offer(v); //初始A及认识的人 Set<Integer> set = new HashSet<>(list.get(ai)); set.add(ai); // A自己也加上 int start = set.size(); // A初始认识的人 while (!queue.isEmpty()) { int u = queue.poll(); for (int v : list.get(u)) { if (set.contains(v)) continue; // 已访问,去重 set.add(v); queue.offer(v); } } System.out.print(set.size() - start); } }
图的数据结构必须邻接矩阵,简直裂开!
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(reader.readLine()); int ai = Integer.parseInt(reader.readLine()); int m = Integer.parseInt(reader.readLine()); int[][] pairs = new int[m][2]; for (int i = 0; i < m; i++) { String[] D = reader.readLine().split(","); pairs[i][0] = Integer.parseInt(D[0]); pairs[i][1] = Integer.parseInt(D[1]); } Map<Integer, ArrayList<Integer>> A = new HashMap<>(); for (int[] pair : pairs) { if (!A.containsKey(pair[0])) { A.put(pair[0], new ArrayList<>()); } if (!A.containsKey(pair[1])) { A.put(pair[1], new ArrayList<>()); } A.get(pair[0]).add(pair[1]); A.get(pair[1]).add(pair[0]); } Queue<Integer> queue = new LinkedList<>(); Set<Integer> hasVisited = new HashSet<>(); queue.add(ai); int res = 0; while (!queue.isEmpty()) { int idx = queue.poll(); if (hasVisited.contains(idx)) { continue; } if (A.get(idx) != null) { for (int num : A.get(idx)) { queue.add(num); } } hasVisited.add(idx); res++; } if (A.get(ai) != null) { res -= A.get(ai).size(); } res-=1; writer.write(String.valueOf(res)); reader.close(); writer.close(); } }
//并查集求联通块内的点数量,但是要减去时间相连的,用hashset存一下就好了 import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class Main { static int N = 10010, n, start, m; static int[] p = new int[N], cnt = new int[N]; static int find(int x){ if(p[x] != x) p[x] = find(p[x]); return p[x]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); start = in.nextInt(); m = in.nextInt(); for(int i = 0; i < n; i ++) p[i] = i; HashSet<Integer> set = new HashSet<>(); Arrays.fill(cnt, 1); for(int i = 1; i <= m; i ++){ String[] arr = in.next().split(","); int a = Integer.parseInt(arr[0]), b = Integer.parseInt(arr[1]); if(a == start || b == start){ set.add(a); set.add(b); } int pa = find(a), pb = find(b); if(pa != pb){ p[pa] = pb; cnt[pb] += cnt[pa]; } } System.out.println( cnt[find(start)] - set.size()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * @Author: coderjjp * @Date: 2020-05-07 19:40 * @Description: 小A最多会新认识的多少人 * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.valueOf(br.readLine()); boolean flag[] = new boolean[n]; int a = Integer.valueOf(br.readLine()); int m = Integer.valueOf(br.readLine()); HashMap<Integer, ArrayList<Integer>> graph = new HashMap<>(); String line[]; int f, t; while (m-- > 0){ line = br.readLine().split(","); f = Integer.valueOf(line[0]); t = Integer.valueOf(line[1]); if (graph.get(f) == null){ ArrayList<Integer> val = new ArrayList<>(); val.add(t); graph.put(f, val); }else graph.get(f).add(t); if (graph.get(t) == null){ ArrayList<Integer> val = new ArrayList<>(); val.add(f); graph.put(t, val); }else graph.get(t).add(f); } // if (graph.get(a) == null){ // System.out.println(0); // return; // } ArrayList<Integer> res = new ArrayList<>(); Queue<Integer> q = new LinkedList<>(); q.offer(a); flag[a] = true; while (!q.isEmpty()){ int node = q.poll(); res.add(node); for (int i : graph.get(node)) if (!flag[i]){ q.offer(i); flag[i] = true; } } System.out.println(res.size() - 1 - graph.get(a).size()); } }