QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411377 | #6765. Don't Really Like How The Story Ends | ChongQY | TL | 40ms | 29272kb | Java8 | 5.3kb | 2024-05-15 12:22:10 | 2024-05-15 12:22:11 |
Judging History
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;
if(a > b){
list[b].add(a);
sets[b].add(a);
} else {
list[a].add(b);
sets[a].add(b);
}
}
for(int i = 1 ; i <= n ; i++) Collections.sort(list[i]);
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;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 40ms
memory: 29272kb
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...