网易 c++
网易笔试编程题 —
#include <iostream> #include <vector> using namespace std; int main(){ int n, m, s; cin >> n >> m >> s; vector<int> v; v.push_back(0); for(int i = 0; i < n; i++){ v.push_back(n-1); } int i; for(i = 0; i < m; i++){ if(v[s] == 0){ break; } int x,y; cin >> x >> y; if(x == 0){ v[y] = 0; }else if(v[y] == 0){ v[x]--; } else{ v[x]--; v[y]--; } } if(i == m){ cout << "0"; }else{ cout << i; } return 0; }
第二道 a了60% 没想出来
#include <iostream> #include <vector> using namespace std; int main(){ int n, m, f; cin >> n >> m >> f; vector<bool> v; for(int i = 0; i < n; i++){ v.push_back(0); } v[f] = 1; bool flag = false; for(int i = 0; i < m; i++){ int num; cin >> num; vector<int> m; for(int j = 0; j < num; j++){ int val; cin >> val; m.push_back(val); if(v[val] == 0){ continue; }else if(v[val] == 1){ flag = true; } } if(flag == true){ for(int k = 0; k < m.size(); k++){ v[m[k]] = 1; } } flag = false; } int count = 0; for(int i = 0; i < v.size(); i++){ if(v[i] == 1){ count++; } } cout << count << endl; return 0; }
第三道不会 第四题不会
第三题渣暴力,四层循环,全过一遍,错了应该,想不出好算法
#include <iostream> #include <vector> using namespace std; int main(){ int n, m, f; cin >> n >> m >> f; vector<bool> v; for(int i = 0; i < n; i++){ v.push_back(0); } v[f] = 1; bool flag = false; for(int i = 0; i < m; i++){ int num; cin >> num; vector<int> m; for(int j = 0; j < num; j++){ int val; cin >> val; m.push_back(val); if(v[val] == 0){ continue; }else if(v[val] == 1){ flag = true; } } if(flag == true){ for(int k = 0; k < m.size(); k++){ v[m[k]] = 1; } } flag = false; } int count = 0; for(int i = 0; i < v.size(); i++){ if(v[i] == 1){ count++; } } cout << count << endl; return 0; }
第四题。。。和问答题第二道没碰。。。对外部排序还是不咋熟