]> Skullheadx's Git Forge - CCC.git/commitdiff
2016 S3 WIP
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sat, 19 Nov 2022 20:53:00 +0000 (15:53 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sat, 19 Nov 2022 20:53:00 +0000 (15:53 -0500)
Main/C++/2016/S3.cpp [new file with mode: 0644]
Main/Python/2016/S3.py [new file with mode: 0644]

diff --git a/Main/C++/2016/S3.cpp b/Main/C++/2016/S3.cpp
new file mode 100644 (file)
index 0000000..86dcf61
--- /dev/null
@@ -0,0 +1,91 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+int main()
+{
+    int N,M;
+    cin >> N >> M;
+    vector<int> pho_restaurants;
+    int t;
+    for (int i = 0; i < M; i++)
+    {
+
+        cin >> t;
+        pho_restaurants.push_back(t);
+    }
+//    for (int i = 0; i < M; i++)
+//    {
+//        cout << pho_restaurants[i] << " ";
+//    }
+//    cout << endl;
+
+    map<int,vector<int>> tree;
+    int a,b;
+    vector<int> temp1{0};
+    for (int i = 0; i < N-1;i++)
+    {
+        cin >> a >> b;
+
+        if ((tree.find(a) == tree.end()))
+        { // not found
+            temp1[0] = b;
+            tree.insert({a,temp1});
+        }
+        else
+        {// found
+            tree[a].push_back(b);
+        }
+        if ((tree.find(b) == tree.end()))
+        { // not found
+            temp1[0]= a;
+            tree.insert({b,temp1});
+        }
+        else
+        {// found
+            tree[b].push_back(a);
+        }
+    }
+    queue<vector<vector<int>>> q;
+    for (int i = 0; i < M; i++)
+    {
+        vector<int> tseen;
+        vector<int> c{pho_restaurants[i]};
+        vector<vector<int>> temp{c,tseen};
+        q.push(temp);
+    }
+    vector<int> current, seen;
+    vector<vector<int>> temp;
+    int last_element;
+    while (!q.empty())
+    {
+        temp = q.front();
+        current = temp[0];
+        last_element = current[current.size()-1];
+        seen = temp[1];
+        q.pop();
+        if (find(pho_restaurants.begin(),pho_restaurants.end(),last_element)!= pho_restaurants.end() && find(seen.begin(),seen.end(),last_element)== seen.end())
+        {
+            seen.push_back(last_element);
+        }
+//        for (int i = 0; i < current.size(); i++)
+//        {
+//            cout << current[i] << " ";
+//        }
+//        cout << endl;
+        if (seen.size() == pho_restaurants.size())
+        {
+            cout << current.size() - 1;
+            break;
+        }
+
+        for (int r = 0; r < tree[last_element].size();r++)
+        {
+            vector<int> c(current.begin(), current.end());
+            c.push_back(tree[last_element][r]);
+            vector<vector<int>> next_value{c,seen};
+            q.push(next_value);
+        }
+    }
+
+    return 0;
+}
diff --git a/Main/Python/2016/S3.py b/Main/Python/2016/S3.py
new file mode 100644 (file)
index 0000000..2bb656a
--- /dev/null
@@ -0,0 +1,38 @@
+from queue import Queue
+N, M = tuple(map(int,input().split()))
+pho_restaurants = tuple(map(int,input().split()))
+
+
+tree = dict()
+for i in range(N-1):
+    a,b = tuple(map(int,input().split()))
+    if a in tree:
+        tree[a].add(b)
+    else:
+        tree[a] = {b}
+    if b in tree:
+        tree[b].add(a)
+    else:
+        tree[b] = {a}
+
+q = Queue()
+for i in pho_restaurants:
+    q.put(([i],set()))
+
+while not q.empty():
+    current, seen = q.get()
+
+    if current[-1] in pho_restaurants:
+        seen.add(current[-1])
+    # print(current, seen, len(pho_restaurants))
+    # if len(current) == 23:
+    #     quit()
+    print(len(current))
+    if len(seen) == len(pho_restaurants):
+        print(len(current)-1)
+        break
+
+    for r in tree[current[-1]]:
+        c = current[:]
+        c.append(r)
+        q.put((c,seen.copy()))