题目链接:Problem - B - Codeforces (Unofficial mirror site, accelerated for Chinese users)
题意:
T组数据。
n和一个长度为n的字符串s(只含><-)(表示一个环,从0开始)。><-分别表示顺逆和双向。
问有多少个点既可以出去,而且出去之后回得来。
题意:给定n条边连接n个点形成一个环,有单向边和双向边,求有多少个点是可达的(即从该点出发回最终能回到该点)
思路:考虑如果所有边都是方向相同的单向边(或用双向边代替)那么n个点都是可达的,否则如果出现两条方向不同的单向边则只需考虑双向边的条数,所有连接了双向边的点就是可达的点,统计即可
import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.file.attribute.AclEntryFlag;
import java.security.AlgorithmConstraints;
import java.sql.Struct;
import java.text.CollationElementIterator;
import java.text.DateFormatSymbols;
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)3e5 + 10;
static math_myself math_me = new math_myself();
static int a[] = new int[N]; // 记录每个点的出度
static int b[]= new int [N]; // 记录每个点的入度
static int vis[] = new int[N]; // 记录每个点可不可以出去又回来
public static void main(String[] args ) throws IOException
{
int T = rd.nextInt();
while(T -- > 0)
{
Arrays.fill(a,0);
Arrays.fill(b,0);
Arrays.fill(vis,0);
int n = rd.nextInt();
char s[] = rd.next().toCharArray();
int x = 0,y = 0;
for(int i=0;i<n;i++)
{
if(s[i]=='-')
{
a[i]++; // 当前点的出度+1
b[(i+1)%n]++; // 下一个点的入度+1
a[(i+1)%n]++; // 下一个点的出度+1
b[i]++; // 当前的入度数+1
}
else if(s[i]=='>')
{
a[i]++; // 当前点的出度+1
b[(i+1)%n]++; // 下一个点的入度+1
x++;
}
else
{
a[(i+1)%n]++; // 下一个点的出度+1
b[i]++; // 当前点的入度+1
y++;
}
}
int ans=0;
if(x > 0 && y > 0)
{
for(int i=0;i<n;i++)
{
if(s[i]=='-') // 双向边
{
vis[i]=1; // 当前点可以出去又回来
vis[(i+1)%n]=1; // 下一个点也可以出去再回来
}
}
for(int i = 0 ; i < n ; i ++) ans += vis[i]; // 答案加上符合条件(可以出去又回来)的点的数量
}
else ans = n; // 当字符全是‘-’,即所有点都可以出去再回来
pw.println(ans);
}
pw.flush();
}
}
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next());}
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class PII
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
}
class math_myself
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
// 判断是否是质数
boolean check_isPrime(int n)
{
if(n < 2) return false;
for(int i = 2 ; i <= n / i; i ++) if (n % i == 0) return false;
return true;
}
}