高级农民
 
- 积分
- 1488
- 学分
- 个
- 大米
- 升
- 人参
- 枚
- 水井
- 尺
- 小麦
- 颗
- 萝卜
- 根
- 小米
- 粒
- UID
- 54047
- 注册时间
- 2012-3-1
- 最后登录
- 1970-1-1
- 在线时间
- 小时
- 好友
- 收听
- 听众
- 日志
- 相册
- 帖子
- 主题
- 分享
- 精华
|
- #include <iostream>
- #include <vector>. 鍥磋鎴戜滑@1point 3 acres
- #include <unordered_map>
- #include <string>.鐣欏璁哄潧-涓浜-涓夊垎鍦
- using namespace std;
- class User { 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
- public:
- string name;
- vector<User*> friends;
- User(string name) {
- this->name = name;
- }
- };
- User *copy(User *u,unordered_map<User*, User*>& mp) {
- if(mp.count(u)) return mp[u]; // alreay grey (in dfs path) or black
-
- // color grey
- mp[u] = new User(u->name+" copy");
- for(auto x:u->friends){
- mp[u]->friends.push_back(copy(x,mp));. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
- }
- return mp[u];
- . visit 1point3acres.com for more.
- //color black
- }
- int main(int argc, const char * argv[]) {
- User* a = new User("a");
- User* b = new User("b");
- User* c = new User("c");
- a->friends.push_back(b);
- b->friends.push_back(c);
- c->friends.push_back(a);
- unordered_map<User*, User*> mp; // user to user copy map "a"->"a copy"
- User* a_copy = copy(a,mp);
- cout<<a_copy->name<<endl;
- cout<<a_copy->friends[0]->name<<endl;.1point3acres缃
- cout<<a_copy->friends[0]->friends[0]->name<<endl;
-
- return 0;
- }
复制代码
a copy
b copy.鐣欏璁哄潧-涓浜-涓夊垎鍦
c copy
Program ended with exit code: 0. 鍥磋鎴戜滑@1point 3 acres
|
|