牛客周赛 Round 117 ABCDE 题解
目录
https://ac.nowcoder.com/acm/contest/121300
A 小红的图
B 小红的菊花
C 小红的好矩阵
D 小红嫁接
E 小红玩马
A 代码
B 代码
C 代码
D 代码
E 代码
A 小红的图
简单判断
一共会输入四种方案
对应输出四种方案
B 小红的菊花
只有一个中心点
其他的点会跟这个点相连
所以记录每个点连接次数
大于两次的点就是我们要找的点
C 小红的好矩阵
这种矩形无非两种情况

如果是第一种情况 那么由于矩形的高是 2 则会牺牲一行 行数的贡献-1
而底是 1 不会牺牲 列全部能贡献
结果就是(n-1)*m
相反第二种 结果是(m-1)*n
D 小红嫁接
要把树变成链
第一个想到那说明叶子节点只能有两个
如何让叶子节点只有两个呢
重要是 我们移动是一个链 类似于红心接龙 在上游的叶子节点进行操作 下游的叶子节点都会变动

如果要是动态去处理 其实还是很复杂

这边我想了很久 也 wa 了几次 最后画了几个链表 发现链表的每个节点最多只能连两个节点

那我们完全可以拿到邻接矩阵
如果一个点有两个以上的邻点 就断开 接到叶子节点上
E 小红玩马
发现 1:马一旦出发 就不能以任意奇数次点步数再回到原点
发现 2:如果马到达一点的步数是奇数 那么它则不可能再以总次数为偶数次到达这个点
发现 3:马到达一个点的总步数只能是奇数次或偶数次
发现 4:马可以到达一个点使用 1 3 5 7 次或 100 102 104 105 次...

我们可以想到 先用 bfs 计算出马到一个点所需使用的最小步数
剩下如果还需要多余的步数
就可以通过在终点和任何一个合法点之间反复横跳来实现

如果 min<k 则无法到达
反复横跳需要的是偶数次步数 则需满足 (k-min)%2==0
这里有一个坑
如果起点是 (1,1)终点(1,1)
那么我们可以通过反复横跳到合法点 去消耗掉多余的步数
我们可以在一开始就消耗掉多余的点 或者在抵达终点后消耗
A 代码
// https://github.com/Dddddduo/acm-java-algorithm
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;// 多多世界第一可爱!
public class Main {static IoScanner sc = new IoScanner();
// static final int mod = (int) (1e9 + 7);
// static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();private static void solve() throws IOException {int l=sc.nextInt();int r=sc.nextInt();if(l==1 && r==1){sc.println("LU");}else if(l==1 && r==2){sc.println("LD");}else if(l==2 && r==1){sc.println("RU");}else if(l==2 && r==2){sc.println("RD");}}public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}sc.flush();sc.bw.close();}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}public void println(int a) throws IOException{print(a);println();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{print(a);println();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{print(a);println();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{print(a);println();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(BigInteger a) throws IOException{bw.write(a.toString());}public void println(BigInteger a) throws IOException{bw.write(a.toString());println();}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{print(a);println();}public void println() throws IOException{bw.newLine();}//其他调试命令:public void flush() throws IOException{//交互题分组调试,或者提前退出的情况下可以先运行此语句再推出bw.flush();return;}public boolean hasNext() throws IOException{//本地普通IDE难以使用这个方法调试,需要按照数据组flush,刷新语句://sc.flush()//调试完可删去return bf.ready();}}
B 代码
// https://github.com/Dddddduo/acm-java-algorithm
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;// 多多世界第一可爱!
public class Main {static IoScanner sc = new IoScanner();
// static final int mod = (int) (1e9 + 7);
// static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();private static void solve() throws IOException {int n = sc.nextInt();int arr[]=new int[n+5];for (int i = 0; i < n - 1; i++) {int u = sc.nextInt();int v = sc.nextInt();arr[u]++;arr[v]++;}int index=-1;for (int i = 0; i < n+5; i++) {if(arr[i]>1){index=i;break;}}sc.println(index);}public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}sc.flush();sc.bw.close();}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}public void println(int a) throws IOException{print(a);println();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{print(a);println();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{print(a);println();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{print(a);println();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(BigInteger a) throws IOException{bw.write(a.toString());}public void println(BigInteger a) throws IOException{bw.write(a.toString());println();}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{print(a);println();}public void println() throws IOException{bw.newLine();}//其他调试命令:public void flush() throws IOException{//交互题分组调试,或者提前退出的情况下可以先运行此语句再推出bw.flush();return;}public boolean hasNext() throws IOException{//本地普通IDE难以使用这个方法调试,需要按照数据组flush,刷新语句://sc.flush()//调试完可删去return bf.ready();}}
C 代码
// https://github.com/Dddddduo/acm-java-algorithm
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;// 多多世界第一可爱!
public class Main {static IoScanner sc = new IoScanner();
// static final int mod = (int) (1e9 + 7);
// static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();private static void solve() throws IOException {long n = sc.nextLong();long m = sc.nextLong();sc.println(n*(m-1)+(n-1)*m);}public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}sc.flush();sc.bw.close();}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}public void println(int a) throws IOException{print(a);println();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{print(a);println();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{print(a);println();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{print(a);println();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(BigInteger a) throws IOException{bw.write(a.toString());}public void println(BigInteger a) throws IOException{bw.write(a.toString());println();}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{print(a);println();}public void println() throws IOException{bw.newLine();}//其他调试命令:public void flush() throws IOException{//交互题分组调试,或者提前退出的情况下可以先运行此语句再推出bw.flush();return;}public boolean hasNext() throws IOException{//本地普通IDE难以使用这个方法调试,需要按照数据组flush,刷新语句://sc.flush()//调试完可删去return bf.ready();}}
D 代码
// https://github.com/Dddddduo/acm-java-algorithm
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;// 多多世界第一可爱!
public class Main {static IoScanner sc = new IoScanner();
// static final int mod = (int) (1e9 + 7);
// static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj ;static int[] degree;private static void solve() throws IOException {n = sc.nextInt();adj= new ArrayList<>();arr = new int[n+5];visited = new boolean[n+5];for (int i = 0; i < n+5; i++) {adj.add(new ArrayList<>());}for (int i = 0; i < n-1; i++) {int u=sc.nextInt();int v=sc.nextInt();adj.get(u).add(v);adj.get(v).add(u);}long count=0;for (ArrayList<Integer> integers : adj) {count+=Math.max(0,integers.size()-2);}sc.println(count);}public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}sc.flush();sc.bw.close();}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}public void println(int a) throws IOException{print(a);println();}public void print(int a) throws IOException{bw.write(String.valueOf(a));}public void println(String a) throws IOException{print(a);println();}public void print(String a) throws IOException{bw.write(a);}public void println(long a) throws IOException{print(a);println();}public void print(long a) throws IOException{bw.write(String.valueOf(a));}public void println(double a) throws IOException{print(a);println();}public void print(double a) throws IOException{bw.write(String.valueOf(a));}public void print(BigInteger a) throws IOException{bw.write(a.toString());}public void println(BigInteger a) throws IOException{bw.write(a.toString());println();}public void print(char a) throws IOException{bw.write(String.valueOf(a));}public void println(char a) throws IOException{print(a);println();}public void println() throws IOException{bw.newLine();}//其他调试命令:public void flush() throws IOException{//交互题分组调试,或者提前退出的情况下可以先运行此语句再推出bw.flush();return;}public boolean hasNext() throws IOException{//本地普通IDE难以使用这个方法调试,需要按照数据组flush,刷新语句://sc.flush()//调试完可删去return bf.ready();}}
E 代码
// https://github.com/Dddddduo/acm-java-algorithmimport java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;// 多多世界第一可爱!
public class Main {static IoScanner sc = new IoScanner();
// static final int mod = (int) (1e9 + 7);
// static final int mod = (int) (998244353);static class Pair {int x, y;// 前一个节点Pair prev;Pair(int x, int y, Pair prev) {this.x = x;this.y = y;this.prev = prev;}}static int n;static int m;static int end_x;static int end_y;public static void solve() throws Exception {n = sc.nextInt();m = sc.nextInt();int k = sc.nextInt();int start_x = sc.nextInt();int start_y = sc.nextInt();end_x = sc.nextInt();end_y = sc.nextInt();Pair end = bfs(start_x, start_y);if (end == null) {System.out.println("No");return;}List<Pair> path = new ArrayList<>();Pair curr = end;while (curr != null) {path.add(curr);curr = curr.prev;}Collections.reverse(path);int d = path.size() - 1;List<Pair> result = new ArrayList<>();for (int i = 1; i < path.size(); i++) {result.add(path.get(i));}// 结果需要k次 最短路径是d次int num = k - d;if(d==0){
// System.out.println(k);if(k%2!=0){sc.println("No");return;}List<Pair> nearOne = getNearOne(start_x, start_y, n, m);if(nearOne==null||nearOne.size()==0){sc.println("No");return;}ArrayList<Pair> ans = new ArrayList<>();for (int i = 0; i < k / 2; i++) {ans.add(nearOne.get(0));ans.add(new Pair(start_x,start_y,null));}sc.println("Yes");for (Pair p : ans) {sc.println(p.x + " " + p.y);}return;}if (num == 0) {sc.println("Yes");for (Pair p : result) {sc.println(p.x + " " + p.y);}} else if (num < 0) {sc.println("No");} else if (num > 0) {if (num % 2 != 0) {sc.println("No");return;}if(result.size()<=3){sc.println("No");return;}ArrayList<Pair> ans = new ArrayList<>();ans.add(path.get(1));ans.add(path.get(2));for (int i = 0; i < num / 2; i++) {ans.add(path.get(1));ans.add(path.get(2));}for (int i = 3; i < path.size(); i++) {ans.add(path.get(i));}sc.println("Yes");for (Pair p : ans) {sc.println(p.x + " " + p.y);}}}// 马的行动路径static int[] dx = {2, 1, -1, -2, -2, -1, 1, 2};static int[] dy = {1, 2, 2, 1, -1, -2, -2, -1};private static Pair bfs(int x1, int y1) {boolean[][] visited = new boolean[n + 5][m + 5];// 已经访问过了visited[x1][y1] = true;Queue<Pair> queue = new LinkedList<>();queue.add(new Pair(x1, y1, null));while (!queue.isEmpty()) {Pair curr = queue.poll();
// sc.println(curr.x + " " + curr.y);// 到达终点if (curr.x == end_x && curr.y == end_y) {return curr;}for (int i = 0; i < 8; i++) {int nx = curr.x + dx[i];int ny = curr.y + dy[i];if (judge(nx, ny, n, m) && !visited[nx][ny]) {visited[nx][ny] = true;queue.add(new Pair(nx, ny, curr));}}}return null;}// 判断点的合法性static boolean judge(int x, int y, int n, int m) {if (x < 1 || x > n || y < 1 || y > m) {return false;} else {return true;}}// 获得这个点可以通过一步到达的地方static List<Pair> getNearOne(int x, int y, int n, int m) {List<Pair> list = new ArrayList<>();for (int i = 0; i < 8; i++) {int nx = x + dx[i];int ny = y + dy[i];if (judge(nx, ny, n, m)) {list.add(new Pair(nx, ny, null));}}return list;}// 判断当前点是否在路径上private static boolean isInPath(Pair p, List<Pair> path) {for (Pair pp : path) {if (pp.x == p.x && pp.y == p.y) {return true;}}return false;}public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}sc.flush();sc.bw.close();}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}public void println(int a) throws IOException {print(a);println();}public void print(int a) throws IOException {bw.write(String.valueOf(a));}public void println(String a) throws IOException {print(a);println();}public void print(String a) throws IOException {bw.write(a);}public void println(long a) throws IOException {print(a);println();}public void print(long a) throws IOException {bw.write(String.valueOf(a));}public void println(double a) throws IOException {print(a);println();}public void print(double a) throws IOException {bw.write(String.valueOf(a));}public void print(BigInteger a) throws IOException {bw.write(a.toString());}public void println(BigInteger a) throws IOException {bw.write(a.toString());println();}public void print(char a) throws IOException {bw.write(String.valueOf(a));}public void println(char a) throws IOException {print(a);println();}public void println() throws IOException {bw.newLine();}//其他调试命令:public void flush() throws IOException {//交互题分组调试,或者提前退出的情况下可以先运行此语句再推出bw.flush();return;}public boolean hasNext() throws IOException {//本地普通IDE难以使用这个方法调试,需要按照数据组flush,刷新语句://sc.flush()//调试完可删去return bf.ready();}}