Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
放份代码,cin cout选手记得关闭同步流In Reply To:答案 Posted by:sigongzi at 2018-05-03 21:33:11 不得不说,这题有毒 先是让我OLE了无数次,我把putchar换成cout了,然后WA,只好对拍,拍出一组 错误原因是删除了某个点后size会减1,导致我遍历不到后面的了 例如YAA VED,我会删掉YAA,然后size = 1,i也指向1,循环结束了,我没更改VED的父亲 cin cout选手记得关闭同步流 #include <iostream> #include <cstdio> #include <vector> #include <set> #include <cstring> #include <string> #include <ctime> #include <map> #define MAXN 200005 #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define ba 823 #define mo 974711 //#define ivorysi using namespace std; typedef long long int64; typedef double db; string str[5005]; int fa[5005]; map<string,int> mmm; vector<int> son[5005]; int N = 1,rt; void dfs(int dep,int x) { if(!x) return; for(int i = 1 ; i <= dep ; ++i) cout<<"+"; cout<<str[x]<<endl; for(int i = 0 ; i < son[x].size() ; ++i) { dfs(dep + 1,son[x][i]); } } void change(int f,int x) { int y = son[x].size() ? son[x].front() : 0; if(y) change(x,y); son[x] = son[f]; if(y == 0) son[x].erase(son[x].begin()); for(int i = 0 ; i < son[x].size() ; ++i) { if(son[x][i] == x) son[x][i] = y; else fa[son[x][i]] = x; } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif ios::sync_with_stdio(false); cin>>str[1];rt = 1; mmm[str[1]] = 1; string s,t; while(cin >> s) { if(s == "print") { dfs(0,rt); for(int i = 1 ; i <= 60 ; ++i) cout<<"-"; cout<<endl; } else if(s == "fire"){ cin>>t; int x = mmm[t]; int y = son[x].size() > 0 ? son[x].front() : 0; for(int i = 0 ; i < son[fa[x]].size() ; ++i) { if(son[fa[x]][i] == x) { if(y == 0) son[fa[x]].erase(son[fa[x]].begin() + i); else {son[fa[x]][i] = y;} break; } } if(!y) continue; else {fa[y] = fa[x];change(x,y);} if(x == rt) rt = y; son[x].clear(); } else { cin >> t;cin >> t; mmm[t] = ++N; str[N] = t; fa[N] = mmm[s]; son[mmm[s]].pb(N); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator