悠悠岁月,不知不觉,距那传说中的pppfish晋级泡泡帝已是过 去数十年。
数十年中,这颗泡泡树上,也是再度变得精彩,各种泡泡天才辈出,惊艳世人,然而,似乎 不论后人如何的出彩,在他们的头 顶之上,依然是有着一道身影而立。
泡泡帝,pppfish。 现在,pppfish即将带着被自己收服的无数个泡泡怪前往下一个 空间。
而在前往下 一个空间的道路上,有N个中转站,和M条空间虫洞连接中转站(双向通道,可有重 边,可有环)
然而,通过虫洞 是要一定的条件的,pppfish将手下所有泡泡怪编号为 1,2 … +∞
对于每个空间虫洞,有两个值L和R,表示此虫洞只允许编号从L到 R的泡 泡怪通过,pppfish现在在1号中转站
他想带尽可能多的泡泡怪到达N号中转站
于是 pppfish找到了机智的你,希望你告诉他最多可以带多少个泡泡怪,同时他还想知道所有泡泡怪的编号(若有多组解取字典序最小的一组 )
30%的数据 1 <= N,M <= 10
100%的数据 2 <= N <= 1000, 0 <= M <= 3000, 1 <= a, b <= N, 1 <= l <= r <= 10^6
显然编号个数是连续且单调的
枚举答案左端点,然后二分右端点,之后从1号点开始dfs,只走包含当前的左右端点的点,判断是否走得到n号点
1 %:pragma GCC optimize(2) 2 %:pragma GCC optimize(3) 3 %:pragma GCC optimize("Ofast") 4 %:pragma GCC optimize("inline") 5 %:pragma GCC optimize("-fgcse") 6 %:pragma GCC optimize("-fgcse-lm") 7 %:pragma GCC optimize("-fipa-sra") 8 %:pragma GCC optimize("-ftree-pre") 9 %:pragma GCC optimize("-ftree-vrp") 10 %:pragma GCC optimize("-fpeephole2") 11 %:pragma GCC optimize("-ffast-math") 12 %:pragma GCC optimize("-fsched-spec") 13 %:pragma GCC optimize("unroll-loops") 14 %:pragma GCC optimize("-falign-jumps") 15 %:pragma GCC optimize("-falign-loops") 16 %:pragma GCC optimize("-falign-labels") 17 %:pragma GCC optimize("-fdevirtualize") 18 %:pragma GCC optimize("-fcaller-saves") 19 %:pragma GCC optimize("-fcrossjumping") 20 %:pragma GCC optimize("-fthread-jumps") 21 %:pragma GCC optimize("-funroll-loops") 22 %:pragma GCC optimize("-fwhole-program") 23 %:pragma GCC optimize("-freorder-blocks") 24 %:pragma GCC optimize("-fschedule-insns") 25 %:pragma GCC optimize("inline-functions") 26 %:pragma GCC optimize("-ftree-tail-merge") 27 %:pragma GCC optimize("-fschedule-insns2") 28 %:pragma GCC optimize("-fstrict-aliasing") 29 %:pragma GCC optimize("-fstrict-overflow") 30 %:pragma GCC optimize("-falign-functions") 31 %:pragma GCC optimize("-fcse-skip-blocks") 32 %:pragma GCC optimize("-fcse-follow-jumps") 33 %:pragma GCC optimize("-fsched-interblock") 34 %:pragma GCC optimize("-fpartial-inlining") 35 %:pragma GCC optimize("no-stack-protector") 36 %:pragma GCC optimize("-freorder-functions") 37 %:pragma GCC optimize("-findirect-inlining") 38 %:pragma GCC optimize("-fhoist-adjacent-loads") 39 %:pragma GCC optimize("-frerun-cse-after-loop") 40 %:pragma GCC optimize("inline-small-functions") 41 %:pragma GCC optimize("-finline-small-functions") 42 %:pragma GCC optimize("-ftree-switch-conversion") 43 %:pragma GCC optimize("-foptimize-sibling-calls") 44 %:pragma GCC optimize("-fexpensive-optimizations") 45 %:pragma GCC optimize("-funsafe-loop-optimizations") 46 %:pragma GCC optimize("inline-functions-called-once") 47 %:pragma GCC optimize("-fdelete-null-pointer-checks") 48 49 #include50 using namespace std; 51 const int N = 1010, M = 3010 * 2; 52 53 int n, m; 54 55 int head[N], rest[M], to[M], l[M], r[M], tot; 56 57 void add(int u, int v, int tl, int tr) { 58 to[++ tot] = v, l[tot] = tl, r[tot] = tr, rest[tot] = head[u], head[u] = tot; 59 } 60 61 struct T { 62 int u, l, r; 63 bool operator < (T t) { 64 return r - l == t.r - t.l ? l > t.l : r - l < t.r - t.l; 65 } 66 } ans; 67 68 int minl = 1e6 + 10, maxr = 0; 69 70 int f[N]; 71 72 int gygy[M], fafa[M]; 73 74 void dfs(int u, int left, int right) { 75 if(f[u]) return ; 76 f[u] = 1; 77 for(int i = head[u] ; i ; i = rest[i]) { 78 int v = to[i]; 79 if(l[i] <= left && right <= r[i]) { 80 dfs(v, left, right); 81 } 82 } 83 } 84 85 bool check(int left, int mid) { 86 for(int i = 1 ; i <= n ; ++ i) f[i] = 0; 87 dfs(1, left, mid); 88 // for(int i = 1 ; i <= n ; ++ i) cout << f[i] << ' '; cout << endl; 89 if(f[n]) { 90 T t = (T) { 0, left, mid }; 91 if(ans < t) ans = t; 92 } 93 return f[n]; 94 } 95 96 void upd(int l) { 97 int ql = 1; 98 int a = 1, b = fafa[0]; 99 while(a <= b) { 100 int m = (a + b) >> 1; 101 if(fafa[m] >= l) ql = m, b = m - 1; 102 else a = m + 1; 103 } 104 int qr = fafa[0]; 105 while(ql <= qr) { 106 int mid = (ql + qr) >> 1; 107 // printf("check: [%d, %d], flag = %d\n", l, fafa[mid], check(l, fafa[mid])); 108 if(check(l, fafa[mid])) ql = mid + 1; 109 else qr = mid - 1; 110 } 111 } 112 113 int main() { 114 freopen("travel.in", "r", stdin); 115 freopen("travel.out", "w", stdout); 116 scanf("%d%d", &n, &m); 117 ans.l = 1, ans.r = 0; 118 for(int i = 1, u, v, l, r ; i <= m ; ++ i) { 119 scanf("%d%d%d%d", &u, &v, &l, &r); 120 add(u, v, l, r); 121 add(v, u, l, r); 122 gygy[++ gygy[0]] = l; 123 fafa[++ fafa[0]] = r; 124 } 125 126 sort(gygy + 1, gygy + 1 + gygy[0]); 127 gygy[0] = unique(gygy + 1, gygy + 1 + gygy[0]) - gygy - 1; 128 129 sort(fafa + 1, fafa + 1 + fafa[0]); 130 fafa[0] = unique(fafa + 1, fafa + 1 + fafa[0]) - fafa - 1; 131 132 // for(int i = 1 ; i <= gygy[0] ; ++ i) cout << gygy[i] << ' '; cout << endl; 133 // for(int i = 1 ; i <= fafa[0] ; ++ i) cout << fafa[i] << ' '; cout << endl; 134 135 for(int i = 1 ; i <= gygy[0] ; ++ i) upd(gygy[i]); 136 137 if(ans.l == ans.r && ans.l == 0) puts("0"); 138 else { 139 printf("%d\n", ans.r - ans.l + 1); 140 for(int i = ans.l ; i <= ans.r ; ++ i) printf("%d ", i); 141 puts(""); 142 } 143 }