#include "kollib.h" #include "message.h" #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Uczen { int num, odl1, num1, odl2, num2; Uczen () {num = odl2 = odl1 = num1 = num2 = 0;} Uczen (int _n, int _n1, int _o1, int _n2, int _o2) {num = _n, num1 = _n1, num2 = _n2, odl1 = _o1, odl2 = _o2;} }; int number, all, n, m, l, p, akt, ojc, nast, nl, np, t1, t2, t3, t4, cel, ret, start; vector <int> Q; vector <Uczen> U; bool is_in (int a) { int l = 0, p = Q.size (), s; while (l <= p) { s = (l+p)/2; if (Q [s] == a) return true; if (Q [s] < a) l = s+1; else p = s-1; } return false; } bool cmp (Uczen a, Uczen b) { return a.num < b.num; } int find (int a) { int l = 0, p = U.size (), s; while (l <= p) { s = (l+p)/2; if (U [s].num == a) return s; if (U [s].num < a) l=s+1; else p=s-1; } printf ("WTFFFFFFFFFFFFFf\n"); return -100; } int min (int a, int b) { return (a<b)?(a):(b); } int main () { n = NumberOfStudents (); m = NumberOfQueries (); number = MyNodeId (); all = NumberOfNodes (); for (int i = 0; i < m; i ++) { Q.push_back (QueryFrom (i+1)); Q.push_back (QueryTo (i+1)); } sort (Q.begin (), Q.end ()); for (int i = 0; i < Q.size ()-1; i ++) if (Q[i] == Q[i+1]) Q.erase (Q.begin () + i--); // if (number == 1) for (int i = 0; i < Q.size (); i ++) printf ("%d\n", Q [i]); for (int i = number; i < Q.size (); i += all) { ojc = Q [i]; akt = FirstNeighbor (Q[i]); l = 1; while (!is_in(akt)) { if (FirstNeighbor (akt) != ojc) nast = FirstNeighbor (akt); else nast = SecondNeighbor (akt); l ++; ojc = akt; akt = nast; } nl = akt; ojc = Q [i]; akt = SecondNeighbor (Q[i]); p = 1; while (!is_in(akt)) { if (FirstNeighbor (akt) != ojc) nast = FirstNeighbor (akt); else nast = SecondNeighbor (akt); p ++; ojc = akt; akt = nast; } np = akt; // printf ("%d --> %d (odl %d), %d (odl %d)\n", Q [i], nl, l, np, p); if (number == 0) U.push_back (Uczen (Q [i], nl, l, np, p)); else { PutInt (0, nl); PutInt (0, l); PutInt (0, np); PutInt (0, p); Send (0); // printf ("wysłałem %d %d %d %d\n", nl, l, np, p); } } if (number == 0) { for (int i = 0; i < Q.size (); i ++) { if (i%all == 0) continue; Receive (i%all); t1 = GetInt (i%all); t2 = GetInt (i%all); t3 = GetInt (i%all); t4 = GetInt (i%all); // printf ("odebrałem %d %d %d %d\n", t1, t2, t3, t4); U.push_back (Uczen (Q [i], t1, t2, t3, t4)); } // for (int i = 0; i < Q.size (); i ++) // printf ("%d -> %d (odl %d), %d (odl %d)\n", U [i].num, U[i].num1, U[i].odl1, U[i].num2, U[i].odl2); sort (U.begin (), U.end (), cmp); int id [408], odl [408], nex [408]; id [0] = U [0].num; odl [0] = U [0].odl1; nex [0] = U [0].num1; ojc = U [0].num; for (int i = 1; i < U.size (); i ++) { akt = find (nex [i-1]); id [i] = U [akt].num; if (U [akt].num1 != ojc) { nex [i] = U [akt].num1; odl [i] = U [akt].odl1; } else { nex [i] = U[akt].num2; odl [i] = U[akt].odl2; } ojc = U [akt].num; } // for (int i = 0; i < U.size (); i ++) // printf ("%d, nastepny %d (odl %d)\n", id [i], nex [i], odl [i]); for (int i = 1; i <= m; i ++) { if (QueryFrom (i) == QueryTo (i)) printf ("0\n"); else { akt = 0; start = QueryFrom (i); cel = QueryTo (i); ret = 0; while (id [akt] != start && id [akt] != cel) akt ++; ret = odl [akt]; akt ++; while (id [akt] != start && id [akt] != cel) { ret += odl [akt]; akt ++; } printf ("%d\n", min(ret, n-ret)); } } } }
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 148 149 150 151 152 153 154 155 156 157 158 | #include "kollib.h" #include "message.h" #include <cstdio> #include <algorithm> #include <vector> using namespace std; struct Uczen { int num, odl1, num1, odl2, num2; Uczen () {num = odl2 = odl1 = num1 = num2 = 0;} Uczen (int _n, int _n1, int _o1, int _n2, int _o2) {num = _n, num1 = _n1, num2 = _n2, odl1 = _o1, odl2 = _o2;} }; int number, all, n, m, l, p, akt, ojc, nast, nl, np, t1, t2, t3, t4, cel, ret, start; vector <int> Q; vector <Uczen> U; bool is_in (int a) { int l = 0, p = Q.size (), s; while (l <= p) { s = (l+p)/2; if (Q [s] == a) return true; if (Q [s] < a) l = s+1; else p = s-1; } return false; } bool cmp (Uczen a, Uczen b) { return a.num < b.num; } int find (int a) { int l = 0, p = U.size (), s; while (l <= p) { s = (l+p)/2; if (U [s].num == a) return s; if (U [s].num < a) l=s+1; else p=s-1; } printf ("WTFFFFFFFFFFFFFf\n"); return -100; } int min (int a, int b) { return (a<b)?(a):(b); } int main () { n = NumberOfStudents (); m = NumberOfQueries (); number = MyNodeId (); all = NumberOfNodes (); for (int i = 0; i < m; i ++) { Q.push_back (QueryFrom (i+1)); Q.push_back (QueryTo (i+1)); } sort (Q.begin (), Q.end ()); for (int i = 0; i < Q.size ()-1; i ++) if (Q[i] == Q[i+1]) Q.erase (Q.begin () + i--); // if (number == 1) for (int i = 0; i < Q.size (); i ++) printf ("%d\n", Q [i]); for (int i = number; i < Q.size (); i += all) { ojc = Q [i]; akt = FirstNeighbor (Q[i]); l = 1; while (!is_in(akt)) { if (FirstNeighbor (akt) != ojc) nast = FirstNeighbor (akt); else nast = SecondNeighbor (akt); l ++; ojc = akt; akt = nast; } nl = akt; ojc = Q [i]; akt = SecondNeighbor (Q[i]); p = 1; while (!is_in(akt)) { if (FirstNeighbor (akt) != ojc) nast = FirstNeighbor (akt); else nast = SecondNeighbor (akt); p ++; ojc = akt; akt = nast; } np = akt; // printf ("%d --> %d (odl %d), %d (odl %d)\n", Q [i], nl, l, np, p); if (number == 0) U.push_back (Uczen (Q [i], nl, l, np, p)); else { PutInt (0, nl); PutInt (0, l); PutInt (0, np); PutInt (0, p); Send (0); // printf ("wysłałem %d %d %d %d\n", nl, l, np, p); } } if (number == 0) { for (int i = 0; i < Q.size (); i ++) { if (i%all == 0) continue; Receive (i%all); t1 = GetInt (i%all); t2 = GetInt (i%all); t3 = GetInt (i%all); t4 = GetInt (i%all); // printf ("odebrałem %d %d %d %d\n", t1, t2, t3, t4); U.push_back (Uczen (Q [i], t1, t2, t3, t4)); } // for (int i = 0; i < Q.size (); i ++) // printf ("%d -> %d (odl %d), %d (odl %d)\n", U [i].num, U[i].num1, U[i].odl1, U[i].num2, U[i].odl2); sort (U.begin (), U.end (), cmp); int id [408], odl [408], nex [408]; id [0] = U [0].num; odl [0] = U [0].odl1; nex [0] = U [0].num1; ojc = U [0].num; for (int i = 1; i < U.size (); i ++) { akt = find (nex [i-1]); id [i] = U [akt].num; if (U [akt].num1 != ojc) { nex [i] = U [akt].num1; odl [i] = U [akt].odl1; } else { nex [i] = U[akt].num2; odl [i] = U[akt].odl2; } ojc = U [akt].num; } // for (int i = 0; i < U.size (); i ++) // printf ("%d, nastepny %d (odl %d)\n", id [i], nex [i], odl [i]); for (int i = 1; i <= m; i ++) { if (QueryFrom (i) == QueryTo (i)) printf ("0\n"); else { akt = 0; start = QueryFrom (i); cel = QueryTo (i); ret = 0; while (id [akt] != start && id [akt] != cel) akt ++; ret = odl [akt]; akt ++; while (id [akt] != start && id [akt] != cel) { ret += odl [akt]; akt ++; } printf ("%d\n", min(ret, n-ret)); } } } } |