P1563 玩具谜题
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct node{int c;string st;}p[500005]; int n,m,dr,tp,k=1; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) cin>>p[i].c>>p[i].st; for(int i=1;i<=m;i++) { scanf("%d%d",&dr,&tp); if(p[k].c==p[1].c) { if(p[1].c==0) if(dr==0) k=k-tp;else k=k+tp; else if(dr==0) k=k+tp;else k=k-tp; while(k<1) k+=n; while(k>n) k-=n; } else { if(p[1].c==0) if(dr==0) k=k+tp;else k=k-tp; else if(dr==0) k=k-tp;else k=k+tp; while(k<1) k+=n; while(k>n) k-=n; } } cout<<p[k].st<<endl; }
P5016 龙虎斗
P5018 对称二叉树
时间复杂度 nlogn
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<vector> using namespace std; int n,ans,size[1000005]; struct Node{ int v,l,r; }a[1000005]; int work(int u){ if(u == -1) return 0; return size[u] = work(a[u].l) + work(a[u].r) + 1; } int dfs(int l, int r){//左右是否对称 if(l == -1 && r == -1) return 1; if(l == -1 || r == -1) return 0;//偏节点 if(a[l].v != a[r].v) return 0; return dfs(a[l].l, a[r].r) && dfs(a[l].r, a[r].l); } int main(){ scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&a[i].v); for(int i = 1; i <= n; i++){ scanf("%d%d",&a[i].l, &a[i].r); } work(1); // cout<<"ddd"<<size[1]<<endl; for(int i = 1; i <= n; i++) if(dfs(a[i].l, a[i].r)) ans = max(ans,size[i]); printf("%d\n",ans); return 0; }