forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
#define MAXN 1005 | ||
|
||
struct edge{ | ||
int to,w; | ||
|
||
edge(){} | ||
edge(int _to, int _w): | ||
to(_to), w(_w){} | ||
}; | ||
|
||
bool ok,visited[MAXN],done[MAXN]; | ||
vector<edge> L[MAXN]; | ||
int h[MAXN],pathxor[MAXN]; | ||
int cont[2]; | ||
|
||
void dfs(int cur, int p, int curh, int val){ | ||
visited[cur] = true; | ||
h[cur] = curh; | ||
bool back = true; | ||
++cont[val]; | ||
|
||
for(int i = L[cur].size() - 1,to,w;i >= 0;--i){ | ||
to = L[cur][i].to; | ||
w = L[cur][i].w; | ||
|
||
if(!visited[to]){ | ||
pathxor[curh + 1] = (pathxor[curh] ^ w); | ||
dfs(to,cur,curh + 1,(val ^ w)); | ||
}else if(!done[to] && (to != p || !back)){ | ||
if((pathxor[curh] ^ pathxor[ h[to] ]) != w) | ||
ok = false; | ||
} | ||
|
||
if(to == p) back = false; | ||
} | ||
|
||
done[cur] = true; | ||
} | ||
|
||
int main(){ | ||
int N; | ||
char s1[10],s2[10],s3[10]; | ||
|
||
while(true){ | ||
scanf("%d",&N); | ||
if(N == 0) break; | ||
|
||
for(int i = 1;i <= N;++i) | ||
L[i].clear(); | ||
|
||
for(int i = 1,v;i <= N;++i){ | ||
scanf("%s %d %s %s",s1,&v,s2,s3); | ||
|
||
if(s3[0] == 'f'){ | ||
L[i].push_back(edge(v,1)); | ||
L[v].push_back(edge(i,1)); | ||
}else{ | ||
L[i].push_back(edge(v,0)); | ||
L[v].push_back(edge(i,0)); | ||
} | ||
} | ||
|
||
memset(visited,false,sizeof visited); | ||
memset(done,false,sizeof done); | ||
pathxor[0] = 0; | ||
ok = true; | ||
|
||
int ans = 0; | ||
|
||
for(int i = 1;i <= N;++i){ | ||
if(!visited[i]){ | ||
cont[0] = cont[1] = 0; | ||
dfs(i,0,0,0); | ||
ans += max(cont[0],cont[1]); | ||
} | ||
} | ||
|
||
if(!ok) puts("Inconsistent"); | ||
else printf("%d\n",ans); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
int main(){ | ||
int T,a,m; | ||
|
||
scanf("%d",&T); | ||
|
||
while(T--){ | ||
scanf("%d %d",&a,&m); | ||
|
||
int ans = -1; | ||
|
||
for(int i = 1;i <= m;++i) | ||
if((a * i - 1) % m == 0){ | ||
ans = i; | ||
break; | ||
} | ||
|
||
if(ans == -1) puts("Not Exist"); | ||
else printf("%d\n",ans); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
#define MAXN 10005 | ||
|
||
struct edge{ | ||
int id,to,l,p; | ||
|
||
edge(){} | ||
edge(int _id, int _to, int _l, int _p): | ||
id(_id), to(_to), l(_l), p(_p){} | ||
}; | ||
|
||
vector<edge> L[MAXN]; | ||
|
||
int memo[MAXN][2]; | ||
|
||
int solve(int cur, int p, int id){ | ||
int &ret = memo[id][(cur < p? 0 : 1)]; | ||
|
||
if(ret == -1){ | ||
ret = 0; | ||
|
||
for(int i = L[cur].size() - 1,to;i >= 0;--i){ | ||
to = L[cur][i].to; | ||
if(to != p) ret = max(ret,L[cur][i].l + solve(to,cur,L[cur][i].id)); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int d[MAXN]; | ||
bool visited[MAXN]; | ||
int mi; | ||
|
||
bool dfs(int cur){ | ||
if(visited[cur]) return false; | ||
if(d[cur] == 1) return true; | ||
visited[cur] = true; | ||
|
||
for(int i = L[cur].size() - 1;i >= 0;--i) | ||
if(L[cur][i].p > mi && dfs(L[cur][i].to)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
int main(){ | ||
int n; | ||
|
||
while(scanf("%d",&n) == 1){ | ||
for(int i = 1;i <= n;++i) L[i].clear(); | ||
memset(d,0,sizeof d); | ||
|
||
for(int i = 1,u,v,l,p;i < n;++i){ | ||
scanf("%d %d %d %d",&u,&v,&l,&p); | ||
L[u].push_back(edge(i,v,l,p)); | ||
L[v].push_back(edge(i,u,l,p)); | ||
++d[u]; ++d[v]; | ||
} | ||
|
||
memset(memo,-1,sizeof memo); | ||
int r = -1,best; | ||
|
||
for(int i = 1;i <= n;++i){ | ||
int cur = 0; | ||
|
||
for(int j = L[i].size() - 1,to;j >= 0;--j){ | ||
to = L[i][j].to; | ||
cur = max(cur,L[i][j].l + solve(to,i,L[i][j].id)); | ||
} | ||
|
||
if(r == -1 || cur < best){ | ||
best = cur; | ||
r = i; | ||
} | ||
} | ||
|
||
int lo = 0,hi = 100000000; | ||
|
||
while(lo < hi){ | ||
mi = (lo + hi) >> 1; | ||
|
||
memset(visited,false,sizeof visited); | ||
|
||
if(dfs(r)) lo = mi + 1; | ||
else hi = mi; | ||
} | ||
|
||
printf("%d\n",lo); | ||
} | ||
|
||
return 0; | ||
} |