G. ABBC or BACB
解题思路
- 一个
可以向左或向右吃掉一段连续的
- 将连续的
合成一个
- 则字符串变为每个
之间被
隔开
- 统计变化后
和
的数量为
- 若
,则
- 若
,则只会大1,即有一段
没被吃掉,则让长度最小的剩下,
- 省略号间的
的个数不影响答案,删去只留两个也一样,
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
//implements Runnable
public class Main {
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int N=2000010;
static int n=0;
static int m=0;
static void solve() throws Exception{
AReader input=new AReader();
// Scanner input=new Scanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String al="abcdefghijklmnopqrstuvwxyz";
char[] ac=al.toCharArray();
int T=input.nextInt();
while(T>0) {
T--;
String string=" "+input.next();
char[] s=string.toCharArray();
n=string.length()-1;
int[] b=new int[n+10];
Arrays.fill(b, -1);
int bb=0;
int[] c=new int[n+10];
int numa=0;
int numb=0;
for(int i=1;i<=n;++i) {
if(s[i]!=s[i-1]) {
bb++;
b[bb]=s[i]-'A';
c[bb]=1;
if(s[i]=='B')numb++;
else numa++;
}else if(s[i]=='B') {
bb++;
b[bb]=s[i]-'A';
c[bb]=1;
numb++;
}else c[bb]++;
}
if(numa<=numb) {
long ans=0;
for(int i=1;i<=bb;++i) {
if(b[i]==0)ans+=c[i];
}
out.println(ans);
}else {
long mi=Linf;
long ans=0;
for(int i=1;i<=bb;++i) {
if(b[i]==0) {
mi=Math.min(mi, c[i]);
ans+=c[i];
}
}
if(mi==Linf)mi=0;
ans-=mi;
out.println(ans);
}
}
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
solve();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
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{
//确定下一个token只有一个字符的时候再用
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 byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}
H. Mad City
解题思路
个点
条边,构成一个只有一个环,其余边形成挂在环上的链
- 考虑什么时候可以无限逃离
- 当
被逮到前跑到环上
-
通过其所在的链到环上,所以
可能在
到环之前跑到
链与环的接点处,守株待兔
- 通过从
跑
找到接点
(第一个被访问两次)
- 然后从
跑最短路,判断
到其距离
,则
无限,反之,则寄
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
//implements Runnable
public class Main {
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int N=2000010;
static int n=0;
static int m=0;
static
class Node{
long x;
long y;
public Node() {
}
public Node(long u,long v) {
x=u;
y=v;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node now = (Node) o;
return x==now.x&&y==now.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
static
class Edge{
int fr,to,nxt;
public Edge(int u,int v) {
fr=u;
to=v;
}
}
static Edge[] e;
static int[] head;
static int cnt=0;
static void addEdge(int fr,int to) {
cnt++;
e[cnt]=new Edge(fr,to);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
static boolean Dij(int s,int a,int b) {
long[] dis=new long[n+1];
Arrays.fill(dis, Linf);
boolean[] vis=new boolean[n+1];
PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
if(o1.y-o2.y>0)return 1;
else if(o1.y-o2.y<0)return -1;
else return 0;
});
q.add(new Node(s,0));
dis[s]=0;
while(!q.isEmpty()) {
Node now=q.peek();q.poll();
int x=(int)now.x;
if(vis[x])continue;
vis[x]=true;
for(int i=head[x];i>0;i=e[i].nxt) {
int v=e[i].to;
if(dis[v]>dis[x]+1) {
dis[v]=dis[x]+1;
q.add(new Node(v,dis[v]));
}
}
}
if(dis[b]<dis[a])return true;
else return false;
}
static boolean[] op;
static int findrt(int x,int fa) {
if(op[x])return x;
op[x]=true;
for(int i=head[x];i>0;i=e[i].nxt) {
int v=e[i].to;
if(v==fa)continue;
int may=findrt(v, x);
if(may!=0)return may;
}
return 0;
}
static void solve() throws Exception{
AReader input=new AReader();
// Scanner input=new Scanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String al="abcdefghijklmnopqrstuvwxyz";
char[] ac=al.toCharArray();
int T=input.nextInt();
while(T>0) {
T--;
n=input.nextInt();
int a=input.nextInt();
int b=input.nextInt();
e=new Edge[2*n+5];
head=new int[n+1];
cnt=0;
op=new boolean[n+1];
for(int i=1;i<=n;++i) {
int u=input.nextInt();
int v=input.nextInt();
addEdge(u, v);
addEdge(v, u);
}
int s=findrt(b, 0);
if(Dij(s, a, b))out.println("YES");
else out.println("NO");
}
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
solve();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
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{
//确定下一个token只有一个字符的时候再用
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 byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}