cf264 div2E题 E. Caisa and Tree(dfs转化为有根树暴力求解)
2014-08-30 22:35
302 查看
点击打开题目链接
题意很简单,直接暴力就可以做了。先dfs把图转化为有根树(记录父亲节点)然后暴力。下面直接贴代码(第一次做出E题,很激动有没有
)
题意很简单,直接暴力就可以做了。先dfs把图转化为有根树(记录父亲节点)然后暴力。下面直接贴代码(第一次做出E题,很激动有没有
)
#include <set> #include <map> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef unsigned long long ull; typedef long long ll; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int maxn = 1e5+10; int pa[maxn],tree[maxn]; int n,q; struct { int to,next; } e[2*maxn]; int head[maxn],tot,cnt; bool vis[maxn]; int gcd(int x,int y) { return y == 0 ? x : gcd (y,x%y); } void init() { memset(head,-1,sizeof(head)); memset(pa,-1,sizeof(pa)); memset(vis,0,sizeof(vis)); tot = 0; } void add(int x,int y) { e[tot].to = y; e[tot].next = head[x]; head[x] = tot++; } void dfs(int r,int p) { pa[r] = p; vis[r] = 1; for (int i = head[r]; ~i; i = e[i].next) { int v = e[i].to; if (!vis[v]) dfs(v,r); } } int main(void) { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); while (~scanf("%d%d",&n,&q)) { for (int i = 1; i <= n; i++) { scanf ("%d",tree+i); } int u,v; init(); for (int i = 0; i < n-1; i++) { scanf("%d%d",&u,&v); add(u,v),add(v,u); } dfs(1,-1); for (int i = 0; i < q; i++) { int d; scanf("%d",&d); if (d==2) { scanf("%d%d",&u,&v); tree[u] = v; } else { int ans = -1; scanf("%d",&u); int tmp = u; while (pa[u] != -1) { u = pa[u]; if ( gcd(tree[u],tree[tmp])>1) { ans = u; break; } } printf("%d\n",ans); } } } return 0; }
相关文章推荐
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
- 【Codeforces Round 272 (Div 2)B】【暴力dfs or 组合数】Dreamoon and WiFi 问号填加减方案数使得最后恰好增量为aim
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree(DFS)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #430 (Div. 2) - C. Ilya And The Tree (树形DFS)
- Codeforces Round #381 (Div. 2) D Alyona and a tree(DFS树上搞事)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set