日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图、思维、入度出度

發(fā)布時間:2024/1/8 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图、思维、入度出度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:Problem - B - Codeforces (Unofficial mirror site, accelerated for Chinese users)

題意:

T組數(shù)據(jù)。

n和一個長度為n的字符串s(只含><-)(表示一個環(huán),從0開始)。><-分別表示順逆和雙向。

問有多少個點既可以出去,而且出去之后回得來。

題意:給定n條邊連接n個點形成一個環(huán),有單向邊和雙向邊,求有多少個點是可達(dá)的(即從該點出發(fā)回最終能回到該點)

思路:考慮如果所有邊都是方向相同的單向邊(或用雙向邊代替)那么n個點都是可達(dá)的,否則如果出現(xiàn)兩條方向不同的單向邊則只需考慮雙向邊的條數(shù),所有連接了雙向邊的點就是可達(dá)的點,統(tǒng)計即可

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]++; // 當(dāng)前點的出度+1b[(i+1)%n]++; // 下一個點的入度+1a[(i+1)%n]++; // 下一個點的出度+1b[i]++; // 當(dāng)前的入度數(shù)+1}else if(s[i]=='>'){a[i]++; // 當(dāng)前點的出度+1b[(i+1)%n]++; // 下一個點的入度+1x++;}else{a[(i+1)%n]++; // 下一個點的出度+1b[i]++; // 當(dāng)前點的入度+1y++;}}int ans=0;if(x > 0 && y > 0){for(int i=0;i<n;i++){if(s[i]=='-') // 雙向邊{vis[i]=1; // 當(dāng)前點可以出去又回來vis[(i+1)%n]=1; // 下一個點也可以出去再回來}}for(int i = 0 ; i < n ; i ++) ans += vis[i]; // 答案加上符合條件(可以出去又回來)的點的數(shù)量}else ans = n; // 當(dāng)字符全是‘-’,即所有點都可以出去再回來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的所有約數(shù)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的情況,這樣會加入兩種情況 ^-^復(fù)雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質(zhì)數(shù)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;} }

總結(jié)

以上是生活随笔為你收集整理的图、思维、入度出度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。