# </>b298: 老闆阿我要退貨 ### 題目 https://zerojudge.tw/ShowProblem?problemid=b298 ![](https://i.imgur.com/2BRDp2u.png) 輸入: 第一行有4個正整數N,M,L,Q(N,L,Q<=10000,M<=100000),N為廠商數量(編號為1~N)接下來M行每行有兩個數字a,b,表示a廠商供應原料給b廠商再接下來L行,每行有一個數字x,表示x廠商是有問題的再接下來Q行每行有一個數字y,詢問y廠商是否有問題 ### stl教學 文一: https://jasonblog.github.io/note/c++/stl_rong_qi_4e0029_-_ji_ben_jie_shao.html 先打開此文 隨時會回看 它是很優秀的stl教學 (Vector,Queue,Stack,Set,Map) ### 看別人的碼 ans1 https://yuihuang.com/zj-b298/ ```cpp= #include <iostream> #include <queue> #include <vector> #include <cstring> using namespace std; vector <int> v[10005]; int a[10005]; queue <int> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ``` vector和queue的部分就開文一看 ### 啊最下面兩行看不懂 so, 解釋from [此文](https://blog.csdn.net/kernelxiao/article/details/108600862) >cin 慢是有原因的,默认情况下,cin 与 stdin 总是保持同步的,也就是说这两种方法可以混用,不必担心文件指针混乱,同时 cout 和 stdout 也一样,两者混用不会输出顺序错乱。正是为了这个兼容的特性,导致 cin 有许多额外的开销,如何禁用这个特性呢?只需一个语句 std::ios::sync_with_stdio(false); ,这样就可以取消 cin 和 stdin 的同步了。 std::ios::sync_with_stdio(); 是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。也就是 C++标准streams(cin,cout,cerr…) 与相应的C标准程序库文件(stdin,stdout,stderr)同步,使用相同的 stream 缓冲区。 默认是同步的,但由于同步会带来某些不必要的负担,因此该函数作用是使得用户可以自行取消同步。 cin.tie(NULL) 取消 cin 和 cout 的绑定。 > **簡單來說:寫這兩行可以讓cin變快。** 那我們繼續 ### part2 ```cpp=13 int n, m, l, Q, b, c; while (cin >> n >> m >> l >> Q){ memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++){ v[i].clear(); //n家廠商 } for (int i = 0; i < m; i++){ cin >> b >> c; v[b].push_back(c); //m對上下關係 } for (int i = 0; i < l; i++){ cin >> b; a[b] = -1; q.push(b); //有病的廠商 } while (!q.empty()){ b = q.front(); q.pop(); for (int i:v[b]){ //i上流廠商 值=下流 if (a[i] == 0){ //if廠商i沒病 a[i] = -1; q.push(i); } } } ``` ### memset介紹 https://shengyu7697.github.io/cpp-memset/ memset(void * ptr, int value, size_t num); 譬如 char str3[] = "hello world"; memset(str3+5, 0, 6); printf("%s\n", str3); –> hello 但! 不能用在int[] 應該用在char (待確認) ### for ( : ) ```cpp vector<int> vec = {1,2,3,4}; int sum = 0; for (int i : vec){ sum += i; } //sum is now 10 ``` 這是一個非常有用的構造,應該在不需要多次使用值索引的情況下使用。 參考 [here](https://www.796t.com/post/bncxNA==.html) 補(順便查到): c++中:和::的用法 http://chiustin.blogspot.com/2017/02/c.html 快了快了,最後 ### part3 ```cpp=38 for (int i = 0; i < Q; i++){ cin >> b; if (a[b] == 0) cout << "YA~~\n"; //沒問題 else cout << "TUIHUOOOOOO\n"; //有病 } } } ``` edit at 2022/10/24 M. 哈哈哈搞了兩節課 ans2 https://alan23273850.github.io/Online-Judge-Problems/zerojudge/b298/