QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411370#6765. Don't Really Like How The Story EndsChongQYTL 43ms40116kbJava85.1kb2024-05-15 12:15:142024-05-15 12:15:16

Judging History

你现在查看的是最新测评结果

  • [2024-05-15 12:15:16]
  • 评测
  • 测评结果:TL
  • 用时:43ms
  • 内存:40116kb
  • [2024-05-15 12:15:14]
  • 提交

answer


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
    public static Read scanner = new Read(System.in);
    //    public static final long MOD = (long) (1e9 + 7);
    public static final long MOD = 998244353;
    public static final int MID = (int) (1 * 1e5 + 100);
    public static final long LMAX = Long.MAX_VALUE;
    public static final long LMIN = Long.MIN_VALUE;
    public static final int IMAX = Integer.MAX_VALUE;
    public static final int IMIN = Integer.MIN_VALUE;
    public static long[] arr = new long[MID];
//    public static long[] Q = new long[MID];
//    public static long[] H = new long[MID];
    public static int N, M , n , m;
    public static long ans;
    //    public static boolean[][] stack = new boolean[MID][MID];
    //四个方向
    public static int[] row = {1, -1, 0, 0};
    public static int[] cos = {0, 0, 1, -1};
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static ArrayList<Integer>[] list = new ArrayList[MID];
    public static HashSet<Integer>[] sets = new HashSet[MID];
    public static boolean[] pool = new boolean[MID];
    public static int iq = 1;
    public static void solve(){
        n = scanner.nextInt();
        int m = scanner.nextInt();
        for(int i = 0 ;  i <= n + 10 ; i++) {
            list[i] = new ArrayList<>();
            sets[i] = new HashSet<>();
            pool[i] = false;
        }
        for(int i = 1 ; i <= m ; i++){
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if(a == b) continue;
            list[a].add(b);
            list[b].add(a);
            sets[a].add(b);
            sets[b].add(a);
        }
        if(n == 1){
            out.println(0);
            return;
        }
        iq = 1;
        ans = 0;
        dfs(1);
        out.println(Math.max(0 , ans));
/*
3
2 3
1 1
1 2
2 1
4 1
1 4
4 2
1 2
3 4
*/
        /*
3 7
2 1
3 3
1 3
1 1
3 2
1 1
3 1

4 8
2 3
4 3
3 2
4 2
1 3
2 1
4 3
2 4
         */
    }
    public static void dfs(int in){
        if(iq > n) return;
        pool[in] = true;
        if(pool[iq]) iq++;
        for(Integer i : list[in]){
            if(i < iq) continue;
            if(pool[i]) continue;
            if(i == iq){
                dfs(i);
            } else {
                if(sets[in].contains(iq)){
                    dfs(iq);
                } else {
                    while(!sets[in].contains(iq)){
                        ans++;
                        dfs(iq);
                    }
                    dfs(iq);
                }
            }
        }
        while(in == 1 && iq <= n){
            ans++;
            dfs(iq);
        }


    }

    public static void main(String[] args) {
        int T = scanner.nextInt();
//        long start = System.currentTimeMillis();//程序开始时间戳
        while (T-- != 0) {
            solve();
        }
        out.flush();
//        long end = System.currentTimeMillis();//程序结束时间戳
//        out.println("运行时间 : " + (end - start));//输出运行时间
    }
    //快读
    static class Read {
        BufferedReader br;
        StringTokenizer st;

        public Read(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in), 16384);
            eat("");
        }

        public void eat(String s) {
            st = new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                System.out.println("            ");
                return null;
            }
        }

        public boolean hasNext() {
            while (!st.hasMoreTokens()) {
                String s = nextLine();
                if (s == null)
                    return false;
                eat(s);
            }
            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

        public BigDecimal nextBigDecimal() {
            return new BigDecimal(next());
        }
    }
    public static void swap(long[] arr , int index1 , int index2){
        long t = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = t;
    }
    public static long Dot_pitch(long x , long y , long xx , long yy){
        return (xx - x) * (xx - x) + (yy - y) * (yy - y);
//        return (long) Math.sqrt(Math.pow(xx - x , 2) + Math.pow(yy - y , 2));
    }
}

class way {
    public int iq ,cnt;

    public way(int iq , int cnt) {
        this.iq = iq;
        this.cnt = cnt;
    }
}

详细

Test #1:

score: 100
Accepted
time: 43ms
memory: 40116kb

input:

3
2 3
1 1
1 2
2 1
4 1
1 4
4 2
1 2
3 4

output:

0
2
1

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

117747
3 7
2 1
3 3
1 3
1 1
3 2
1 1
3 1
4 8
2 3
4 3
3 2
4 2
1 3
2 1
4 3
2 4
3 4
2 3
2 2
3 3
1 1
2 5
1 1
2 2
2 2
1 2
2 2
3 7
2 1
1 2
3 3
3 2
1 2
3 3
3 2
4 5
1 2
3 3
4 4
1 4
2 1
3 1
3 2
1 3
1 1
1 1
1 1
1 6
1 1
1 1
1 1
1 1
1 1
1 1
5 4
2 1
2 5
1 3
3 2
4 7
1 1
2 4
3 2
1 1
1 1
4 2
2 3
5 8
3 3
2 2
4 2
1 4
1...

output:


result: