2020/8/27
英语四级
cf四题
3题
#include"stdio.h" #include"string.h" #include"stack" #include"map" #include"math.h" #include"vector" #include"queue" #include"algorithm" using namespace std; #define OK printf("\n"); #define Debug printf("this_ok\n"); typedef long long ll; #define scanll(a,b) scanf("%lld%lld",&a,&b); #define scanl(a) scanf("%lld",&a); #define printl(a,b) {if(b == 0) {printf("%lld ",a);} else{ printf("%lld\n",a);}} #define print_int(a,b) {if(b == 0) printf("%d ",a); else printf("%d\n",a);} const ll mod=998244353; typedef pair<int,int> PII; inline int read(){ int s = 0, w = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); } return s * w; } const int N = 201010; const int dirx[4] = {0,1,0,-1}; const int diry[4] = {-1,0,1,0}; int head[N],ver[N << 1],Next[N << 1],tot; int dfn[N],low[N]; int Stack[N],ins[N],c[N]; vector<int>scc[N]; int n,m,top,num,cnt; int dp[N],a[N]; int b[N]; int hc[N],vc[N << 1],nc[N << 1],tc; int p[2010][2010]; vector<int>G[N],ans[N]; void add_c(int x,int y){ vc[++ tc] = y; nc[tc] = hc[x]; hc[x] = tc; } void add(int x,int y){ ver[++ tot] = y; Next[tot] = head[x]; head[x] = tot; } void tarjan(int x){ dfn[x] = low[x] = ++ num; Stack[++ top] = x;ins[x] = 1; for(int i = head[x]; i; i = Next[i]) if(!dfn[ver[i]]) { tarjan(ver[i]); low[x] = min(low[x],low[ver[i]]); } else if(ins[ver[i]]) low[x] = min(low[x],dfn[ver[i]]); if(dfn[x] == low[x]) { cnt ++; int y; do{ y = Stack[top --]; ins[y] = 0; c[y] = cnt;scc[cnt].push_back(y); b[cnt] += a[y]; } while(x != y); } } int main() { n = read(); for(int i = 1; i <= n; i ++){ int k = read(); while(k --){ int x = read(); G[i].push_back(x); p[i][x] = 1; } } for(int i = 1; i <= n; i ++) b[i] = read(); for(int i = 1; i <= n; i ++){ for(int j = 0; j < G[i].size(); j ++){ int y = G[i][j]; if(b[i] == y) add(y + n,i); else add(i,y + n); } } for(int i = 1; i <= n * 2; i ++) if(!dfn[i]) tarjan(i); for(int x = 1; x <= n; x ++) for(int y = 1; y <= n; y ++){ if(b[x] == y || (c[x] == c[y + n] && p[x][y])) ans[x].push_back(y); } for(int i = 1; i <= n; i ++){ printf("%d",ans[i].size()); for(int j = 0; j < ans[i].size(); j ++) printf(" %d",ans[i][j]); printf("\n"); } } /* 5 2 1 4 5 5 5 2 1 5 5 3 */ #include"stdio.h" #include"string.h" #include"stack" #include"map" #include"math.h" #include"vector" #include"queue" #include"algorithm" using namespace std; #define OK printf("\n"); #define Debug printf("this_ok\n"); typedef long long ll; #define scanll(a,b) scanf("%lld%lld",&a,&b); #define scanl(a) scanf("%lld",&a); #define printl(a,b) {if(b == 0) {printf("%lld ",a);} else{ printf("%lld\n",a);}} #define print_int(a,b) {if(b == 0) printf("%d ",a); else printf("%d\n",a);} const ll mod=998244353; typedef pair<int,int> PII; inline int read(){ int s = 0, w = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); } return s * w; } const int N = 201010; const int dirx[4] = {0,1,0,-1}; const int diry[4] = {-1,0,1,0}; int head[N],ver[N << 1],Next[N << 1],tot; int dfn[N],low[N]; int Stack[N],ins[N],c[N]; vector<int>scc[N]; int n,m,top,num,cnt; int dp[N],a[N]; int b[N]; int hc[N],vc[N << 1],nc[N << 1],tc; int p[2010][2010]; int vis[N]; vector<int>G[N],ans[N]; int s[N]; void add_c(int x,int y){ vc[++ tc] = y; nc[tc] = hc[x]; hc[x] = tc; } void add(int x,int y){ ver[++ tot] = y; Next[tot] = head[x]; head[x] = tot; } void dfs(int x){ while(head[x]) { int y = ver[head[x]]; head[x] = Next[head[x]]; dfs(y); } s[++top] = x; } int main() { n = read();tot = 1; int t = (1 << (n - 1)) - 1; for(int i = 0; i < (1 << (n - 1)); i ++){ add(i, ((i << 1) & t) | 1); add(i, (i << 1) & t); } dfs(0); printf("%d ",1 << n); while(top > 1){ printf("%c",(s[top --] >> (n - 2)) + '0'); } printf("\n"); } /* 5 2 1 4 5 5 5 2 1 5 5 3 */
#include"stdio.h" #include"string.h" #include"stack" #include"map" #include"math.h" #include"iostream" #include"vector" #include"queue" #include"algorithm" using namespace std; #define OK printf("\n"); #define Debug printf("this_ok\n"); #define inf 1e9 #define INF 1e18 typedef long long ll; #define scanll(a,b) scanf("%lld%lld",&a,&b); #define scanl(a) scanf("%lld",&a); #define printl(a,b) if(b == 0) printf("%lld ",a); else printf("%lld\n",a); #define print_int(a,b) if(b == 0) printf("%d ",a); else printf("%d\n",a); typedef pair<int,int> PII; inline int read(){ int s = 0, w = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); } return s * w; } const ll mod = 998244353; const int N = 301000 * 30; const double pi = acos(-1); int n,m; int root[N],lc[N],rc[N],tot; int far[N],dep[N]; void Build_Tree(int &rt,int l,int r){ rt = ++ tot; if(l == r) { far[rt] = l; return ; } int mid = (l + r) >> 1; Build_Tree(lc[rt],l,mid); Build_Tree(rc[rt],mid + 1,r); } void Merge(int last,int &rt,int l,int r,int pos,int Fa){ rt = ++ tot; lc[rt] = lc[last]; rc[rt] = rc[last]; if(l == r){ far[rt] = Fa; dep[rt] = dep[last]; return ; } int mid = (l + r) >> 1; if(pos <= mid) Merge(lc[last],lc[rt],l,mid,pos,Fa); else Merge(rc[last],rc[rt],mid + 1,r,pos,Fa); } void Update(int rt,int l,int r,int pos){ if(l == r) { dep[rt] ++; return ; } int mid = (l + r) >> 1; if(pos <= mid) Update(lc[rt],l,mid,pos); else Update(rc[rt],mid + 1,r,pos); } int query(int rt,int l,int r,int pos){ if(l == r){ return rt; } int mid = (l + r) >> 1; if(pos <= mid) query(lc[rt],l,mid,pos); else query(rc[rt],mid + 1,r,pos); } int Find(int rt,int pos){ int now = query(rt,1,n,pos); // printf("now = %d pos= %d farx = %d\n",now,pos,far[now]); if(far[now] == pos) return now; return Find(rt,far[now]); } int main(){ n = read(); m = read(); Build_Tree(root[0],1,n); int now = 0; while(m --){ int op = read(); if(op == 1) { int x = read(),y = read(); root[now + 1] = root[now]; ++ now; int posx = Find(root[now],x); int posy = Find(root[now],y); if(far[posx] == far[posy]) continue; if(dep[posx] > dep[posy]) swap(posx,posy); Merge(root[now - 1],root[now],1,n,far[posx],far[posy]); if(dep[posx] == dep[posy]) Update(root[now],1,n,far[posy]); } else if(op == 2){ int k = read(); root[now + 1] = root[k]; now ++; } else if(op == 3){ int x = read(),y = read(); root[now + 1] = root[now]; now ++; int posx = Find(root[now],x); int posy = Find(root[now],y); if(far[posx] == far[posy]) printf("1\n"); else printf("0\n"); } } }