Codeforces Round #383 (Div. 1)
2016-12-08 12:07
309 查看
A:
题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x。
题解:
首先每个点应该都在一个环上,否则无解。
对于大小为k的奇环上的点,满足要求的最小的t是k.
对于大小为k的偶环上的点,满足要求的最小的t是k/2.
对于每个环求最小公倍数即可。 数据范围很小,直接暴力求环就可以了。
代码:
View Code
题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x。
题解:
首先每个点应该都在一个环上,否则无解。
对于大小为k的奇环上的点,满足要求的最小的t是k.
对于大小为k的偶环上的点,满足要求的最小的t是k/2.
对于每个环求最小公倍数即可。 数据范围很小,直接暴力求环就可以了。
代码:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <cstdlib> #include <set> #include <queue> using namespace std; #define X first #define Y second #define Mod 1000000007 #define N 200110 #define M 200110 typedef long long ll; typedef pair<int,int> pii; const ll INF=4e18; int n; vector<int> g ; int color ,a ,b ; bool Dfs(int x,int c) { color[x]=c; for (int i=0;i<g[x].size();i++) { int y=g[x][i]; if (!color[y] && !Dfs(y,3-c)) return false; else if (color[y]==color[x]) return false; } return true; } int main() { //freopen("in.in","r",stdin); //freopen("out.out","w",stdout); scanf("%d",&n); for (int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); a[i]=x,b[i]=y; g[x].push_back(y); g[y].push_back(x); } for (int i=1;i<=n;i++) g[2*i-1].push_back(2*i),g[2*i].push_back(2*i-1); for (int i=1;i<=2*n;i++) if (!color[i]) Dfs(i,1); for (int i=1;i<=n;i++) printf("%d %d\n",color[a[i]],color[b[i]]); return 0; }
View Code
相关文章推荐
- Codeforces Round #383 (Div. 2) A. Arpa’s hard exam and Mehrdad’s naive cheat(快速幂水题)
- Codeforces Round #383 (Div. 2) B
- Codeforces Round #383 (Div. 2) B
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrda 4000 d's valuable Hoses —— DP(01背包)
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution(STL乱搞)
- Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2) A.Arpa’s hard exam and Mehrdad’s naive cheat【循环节/快速幂】
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
- Codeforces Round #383 (Div. 2) 742A Arpa’s hard exam and Mehrdad’s naive cheat
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution 数学
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- Codeforces Round #403 div2 B. The Meeting Place Cannot Be Changed
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- Codeforces Round #368 (Div. 2)
- Codeforces Round #324 (Div. 2) A. Olesya and Rodion 水题
- Codeforces Round #Pi (Div. 2) B Berland National Library
- Codeforces Round #324 (Div. 2) Kolya and Tanya 组合数学
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya(组合数学)