牛客網 程式自動分析 並查集 離散化

2021-09-23 18:31:44 字數 806 閱讀 5417

第一眼看這道題秒速寫並查集然後wa了,仔細看資料嚇了一跳,1e9的數太大,而且數字之間不需要確切的比較,只需要清楚他們的大小關係。所以,對他們進行離散化就ok

下面是ac**:

#includeusing namespace std;

const int maxn = 1e6 + 5;

int p[maxn], book[maxn], n, res,t; // p和並查集一塊用,book表示離散化後的陣列

bool flag;

struct node

d[maxn];

bool cmp (node a, node b)

void init()

int find_(int x)

void unite(int x, int y)

int main()

sort (book, book + tot); // 先排序

res = unique (book, book + tot) - book; // 再去重

for (int i = 1; i <= n; i++)

sort (d + 1, d + n + 1, cmp); // 排序

for (int i = 1; i <= res; i++) p[i] = i;

for (int i = 1; i <= n; i++)

}if (flag) cout << "yes" << endl;

else cout << "no" << endl;

}return 0;

}

237 程式自動分析 離散化 並查集

鏈結 題目描述 在實現程式自動分析的過程中,常常需要判定一些約束條件是否能被同時滿足。考慮乙個約束滿足問題的簡化版本 假設x1,x2,x3,代表程式 現的變數,給定n個形如xi xj或xi xj的變數相等 不等的約束條件,請判定是否可以分別為每乙個變數賦予恰當的值,使得上述所有約束條件同時被滿足。例...

並查集 程式自動分析

原題鏈結 規模太大,先用離散化縮小。考慮所有相等條件,把所有相等的變數 進同乙個集合中。考慮所有不等條件,若兩個變數處於同一集合中,則證明無法同時滿足這兩個條件。實現 include define n 100000 10 using namespace std int n,m int a 2 n f...

並查集 程式自動分析

給你兩個邏輯關係,判斷是否有衝突 先將兩個邏輯分開來看,前乙個邏輯存在那麼後乙個邏輯一定不存在,如果存在那麼答案就錯誤了,可以用並查集維護,詳細見 define fre yes include include include const int n 1000005 struct message ar...