-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_spreading.cpp
More file actions
147 lines (119 loc) · 3.48 KB
/
node_spreading.cpp
File metadata and controls
147 lines (119 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//Adam Rosenberg
include <iostream>
#include <vector>
#include <list>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
/**
Given a graph, where nodes are pushing out messages to each child node, record all of the name names of the nodes in
lexicographical order efficiently.
**/
struct Student{
public:
string name;
vector<Student*> friends;
int uid;
// map<string, Student*> friendsmap;
Student(string _name){
name = _name;
}
};
map<string, Student*> studentmap;
vector<string> studentnames;
int day = 0;
int numberofstudents;
void BFS(string name){
int maxday = 0;
set<string>unpopularfriends;
for(int i = 0; i < numberofstudents; i++){
unpopularfriends.insert(studentnames.at(i));
}
queue<Student*> queue;
map<int, vector<string>> final;
Student* current = studentmap[name];
vector<int> distance(numberofstudents, -1);
distance.at(current->uid) = 0;
queue.push(current);
final[0].push_back(current->name);
unpopularfriends.erase(current->name);
while(!queue.empty()){
Student* curr = queue.front();
queue.pop();
for(auto s:curr->friends){
int friendDist = distance.at(s->uid);
if(friendDist == -1){
unpopularfriends.erase(s->name);
queue.push(s);
distance.at(s->uid) = distance.at(curr->uid) + 1;
maxday = distance.at(curr->uid) + 1;
final[distance.at(s->uid)].push_back(s->name);
}
}
}
for(int i = 0; i <= maxday ; i++) {
sort(final[i].begin(), final[i].end());
}
for(int i = 0; i <= maxday; i++){
for(int j = 0; j < final[i].size(); j++){
cout << final[i].at(j) << " ";
}
}
vector<string> leftofnames;
for(auto n:unpopularfriends){
leftofnames.push_back(n);
}
sort(leftofnames.begin(), leftofnames.end());
for(int i = 0; i < leftofnames.size(); i++){
cout << leftofnames.at(i) << " ";
}
cout << endl;
}
int main() {
string buffer;
getline(cin, buffer);
numberofstudents = stoi(buffer);
for(int i = 0; i<numberofstudents; i++){
string name;
getline(cin, name);
Student* current = new Student(name);
current->uid = i;
studentmap[name] = current;
studentnames.push_back(name);
}
int numberoffriendships;
getline(cin, buffer);
numberoffriendships = stoi(buffer);
for(int i = 0; i < numberoffriendships; i++){
getline(cin, buffer);
stringstream ss(buffer);
bool flag = true;
string src;
string dest;
while(ss.good()){
if(flag){
ss>>src;
flag = !flag;
}else{
ss>>dest;
}
}
Student* destpointer = studentmap[dest];
Student* sourcepointer = studentmap[src];
destpointer->friends.push_back(sourcepointer);
sourcepointer->friends.push_back(destpointer);
}
int numberofrumors;
getline(cin, buffer);
numberofrumors = stoi(buffer);
for(int i = 0; i < numberofrumors; i++){
getline(cin, buffer);
BFS(buffer);
}
// for(auto s : studentmap)
// cout<< s.second->name << endl;
return 0;
}