hoj 13788 Dwarves
生活随笔
收集整理的這篇文章主要介紹了
hoj 13788 Dwarves
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//首先判環(huán)我都不會 dfs實(shí)在寫的戳 后來測了好多數(shù)據(jù)發(fā)現(xiàn)dfs寫錯了 唉 其次不會哈希啊 這個哈希還是dalao手寫給我的 珍惜!!!
#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <string> #include <string.h> #include <queue> #include <math.h> #include <map> #define ULL unsigned long long using namespace std; const int maxn=10010; const int INF=0x3f3f3f3f; #define mod 10007 int cnt; vector <int> e[10010];//map<ULL,int> mp; int f[10010]; int de[maxn]; int tot; bool flag=true; void dfs(int now){f[now]=-1;for(int i=0;i<e[now].size();i++){if(f[e[now][i]]==-1) {flag=false;}else if(f[e[now][i]]==0) dfs(e[now][i]);}f[now]=1; }const int maxbuf = 11111; struct node {ULL key;int id,next; }buf[maxbuf]; int h[maxn]; ULL fuck(char a[30]){ULL ha=0;for(int i=0;a[i];i++){ha*=52;ha+=a[i];}return (ha); } void inithash() {tot = 0;memset(h,-1,sizeof(h)); } int ins(ULL key) {int now = ++tot;buf[now].key = key, buf[now].id = now, buf[now].next = h[key%mod];h[key%mod] = now;return now; } int fnd(ULL key) {for (int i=h[key%mod];~i;i=buf[i].next) {if (buf[i].key == key) return buf[i].id;}return 0; }char s1[30],s2[30]; int main(){#ifdef shuaishuaifreopen("C:\\Users\\hasee\\Desktop\\trianglen=100000.in","r",stdin);// freopen("C:\\Users\\hasee\\Desktop\\b.txt","w",stdout);// freopen("C:\\Users\\hasee\\Desktop\\picture.in","r",stdin); #endif// ios::sync_with_stdio(false);char s;int n;memset(f,0,sizeof f);memset(de,0,sizeof de);inithash();while(scanf("%d\n",&n)!=EOF){cnt=1;for(int i=0;i<n;i++){// cout<<"@"<<endl;scanf("%s %c %s\n",s1,&s,s2); // cout<<s1<<" "<<s<<" "<<s2<<endl; ULL x1=fuck(s1);ULL x2=fuck(s2);int u = fnd(x1), v = fnd(x2);// cout<<"before "<<u<<" "<<v<<" "<<tot<<endl; // cout<<x1<<" "<<x2<<endl;if (!u) u = ins(x1);if (!v) v = ins(x2); // printf("%s %d %s %d\n",s1,u,s2,v); // cout<<u<<" "<<v<<" "<<tot<<endl;// printf("%d %d\n",x1,x2);// int x1=mp[t1],x2=mp[t2]; // if(x1==0)x1=mp[t1]=cnt++; // if(x2==0)x2=mp[t2]=cnt++;if(s=='>'){e[u].push_back(v);de[v]++;}else{e[v].push_back(u);de[u]++;}}for(int i=1;i<=tot;i++ ){if(f[i]==0) dfs(i);if(!flag) break;}if(flag)printf("possible\n");elseprintf("impossible\n");for(int i=0;i<=tot;i++)e[i].clear(),f[i]=0,h[i]=-1,de[i]=0;tot=0;}return 0; }
#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <string> #include <string.h> #include <queue> #include <math.h> #include <map> #define ULL unsigned long long using namespace std; const int maxn=10010; const int INF=0x3f3f3f3f; #define mod 10007 int cnt; vector <int> e[10010];//map<ULL,int> mp; int f[10010]; int de[maxn]; int tot; bool flag=true; void dfs(int now){f[now]=-1;for(int i=0;i<e[now].size();i++){if(f[e[now][i]]==-1) {flag=false;}else if(f[e[now][i]]==0) dfs(e[now][i]);}f[now]=1; }const int maxbuf = 11111; struct node {ULL key;int id,next; }buf[maxbuf]; int h[maxn]; ULL fuck(char a[30]){ULL ha=0;for(int i=0;a[i];i++){ha*=52;ha+=a[i];}return (ha); } void inithash() {tot = 0;memset(h,-1,sizeof(h)); } int ins(ULL key) {int now = ++tot;buf[now].key = key, buf[now].id = now, buf[now].next = h[key%mod];h[key%mod] = now;return now; } int fnd(ULL key) {for (int i=h[key%mod];~i;i=buf[i].next) {if (buf[i].key == key) return buf[i].id;}return 0; }char s1[30],s2[30]; int main(){#ifdef shuaishuaifreopen("C:\\Users\\hasee\\Desktop\\trianglen=100000.in","r",stdin);// freopen("C:\\Users\\hasee\\Desktop\\b.txt","w",stdout);// freopen("C:\\Users\\hasee\\Desktop\\picture.in","r",stdin); #endif// ios::sync_with_stdio(false);char s;int n;memset(f,0,sizeof f);memset(de,0,sizeof de);inithash();while(scanf("%d\n",&n)!=EOF){cnt=1;for(int i=0;i<n;i++){// cout<<"@"<<endl;scanf("%s %c %s\n",s1,&s,s2); // cout<<s1<<" "<<s<<" "<<s2<<endl; ULL x1=fuck(s1);ULL x2=fuck(s2);int u = fnd(x1), v = fnd(x2);// cout<<"before "<<u<<" "<<v<<" "<<tot<<endl; // cout<<x1<<" "<<x2<<endl;if (!u) u = ins(x1);if (!v) v = ins(x2); // printf("%s %d %s %d\n",s1,u,s2,v); // cout<<u<<" "<<v<<" "<<tot<<endl;// printf("%d %d\n",x1,x2);// int x1=mp[t1],x2=mp[t2]; // if(x1==0)x1=mp[t1]=cnt++; // if(x2==0)x2=mp[t2]=cnt++;if(s=='>'){e[u].push_back(v);de[v]++;}else{e[v].push_back(u);de[u]++;}}for(int i=1;i<=tot;i++ ){if(f[i]==0) dfs(i);if(!flag) break;}if(flag)printf("possible\n");elseprintf("impossible\n");for(int i=0;i<=tot;i++)e[i].clear(),f[i]=0,h[i]=-1,de[i]=0;tot=0;}return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/MeowMeowMeow/p/7277017.html
總結(jié)
以上是生活随笔為你收集整理的hoj 13788 Dwarves的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [补档]暑假集训D2总结
- 下一篇: mongodb 启动方法