コード例 #1
0
 int findCelebrity(int n) {
     int l = 0, r = n - 1;
     while(l < r)
     {
         if(knows(l, r))
         {
             l++;
         }
         else
         {
             r--;
         }
     }
     
     for(int i = 0;i < n;i++)
     {
         if(i == l)
         {
             continue;
         }
         
         if(knows(l, i) || !knows(i, l))
         {
             return -1;
         }
     }
     
     return l;
 }
コード例 #2
0
ファイル: qt_find_the_celebrity.cpp プロジェクト: qiaotian/OJ
 int findCelebrity(int n) {
     if(n<=1) return n;
     int candidate = 0;
     for(int i=1; i<n; i++){
         if (!knows(i,candidate)) candidate = i;
     }
     for(int j=0; j<n; j++) {
         if(j== candidate) continue;
         if(!knows(j,candidate) || knows(candidate,j))return -1;
     }
     return candidate;
 }
コード例 #3
0
ファイル: 277.cpp プロジェクト: applelooking/leetcode
 int findCelebrity(int n) {
     int result = 0;
     for (int i = 1;i < n;++i) {
         if (knows(result, i)) {
             result = i;
         }
     }
     for (int i = 0;i < n;++i) {
         if (i == result) continue;
         if (knows(result, i) || !knows(i, result)) return -1;
     }
     return result;
 }
コード例 #4
0
 int findCelebrity(int n) {
     int res = 0;
     //find celebrity candidates.
     for (int i = 0; i < n; ++i) {
         if (knows(res, i)) res = i;
     }
     
     //Verify candidates.
     for (int i = 0; i < n; ++i) {
         if (res != i && (knows(res, i) || !knows(i, res))) return -1;
     }
     return res;
 }
コード例 #5
0
 int findCelebrity(int n) {
     if (n <= 1) return n;
     int cur = 0;
     for (int i = 1; i < n; i ++) {
     	if (!knows(i, cur)) {
     		cur = i;
     	}
     }
     for (int i = 0; i < n; i ++) {
     	if (i == cur) continue;
     	if (!knows(i, cur) || knows(cur, i))  //if i does not know candidate, or candidate knows i, return -1;
     		return -1;
     }
     return cur;
 }
コード例 #6
0
ファイル: Host.cpp プロジェクト: IngwiePhoenix/IceTea
void Host::registerType(const std::string& type, unsigned int version, unsigned int lowestVersion){
    if (!knows(type)){
        ProviderInfo pi;
        pi.version = version;
        pi.lowestVersion = lowestVersion;
        knownTypes[type] = pi;
    }
}
コード例 #7
0
 int findCelebrity(int n) {
     int cand = 0;
     for (int i = 1; i < n; i++) {
         // if cand knows i, cand should not be the 
         // candidate celebrity. If cand doesn't know i, 
         // then i is not the celebrity.
         if (knows(cand, i)) {
             cand = i;
         } else {
             // do nothing.
         }
     }
     
     // verify cand.
     for (int i = 0; i < n; i++) {
         if (i != cand && (knows(cand, i) || !knows(i, cand))) {
             return -1;
         }
     }
     return cand;
 }
コード例 #8
0
    int findCelebrity(int n) {
        int cel = 0;
        
        for(int i = 1; i < n; ++i){
            if(!knows(i, cel)) cel = i;
        }
        
        for(int i = 0; i < cel; ++i){
            if(knows(cel,i) || !knows(i,cel)){
                return -1;
            }
        }

        for(int i = cel+1; i < n; ++i){
            if(knows(cel,i)){
                return -1;
            }
        }
        
        return cel;
    }
コード例 #9
0
SUMOReal
RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
    SUMOReal ret = 0;
    if (knows(id)) {
        const std::vector<FlowDef>& flows = getFlowDefs(id);
        for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
            ret += (*i).qPKW;
            ret += (*i).qLKW;
        }
    }
    return ret;
}
コード例 #10
0
ファイル: Host.cpp プロジェクト: IngwiePhoenix/IceTea
bool Host::validateProvider(Provider* provider) const{
    const std::string& type = provider->plumaGetType();
    if ( !knows(type) ){
        fprintf(stderr, "%s provider type isn't registered.\n", type.c_str());
        return false;
    }
    if (!provider->isCompatible(*this)){
        fprintf(stderr, "Incompatible %s provider version.\n", type.c_str());
        return false;
    }
    return true;
}
コード例 #11
0
ファイル: qt_find_the_celebrity.cpp プロジェクト: qiaotian/OJ
 int findCelebrity(int n) {
     if(n==0 || n==1) return -1;
     int count = n; // 初始化待定人员数目
     vector<int> flag;
     for(int i=0; i<n; i++) flag.push_back(i);
     while(count>1) {
         int tmp = count;
         for(int i=0; i<tmp/2; i++) {
             if(knows(flag[2*i], flag[2*i+1])) {
                 flag[i] = flag[2*i+1];
             } else {
                 flag[i] = flag[2*i];
             }
             count--;
         }
         if(tmp%2) flag[tmp/2] = flag[tmp-1];
     }
     for(int i=0; i<n; i++) {
         if(i==flag[0]) continue;
         if(!knows(i, flag[0]) || knows(flag[0], i)) return -1;
     }
     return flag[0];
 }
コード例 #12
0
 int findCelebrity(int n) {
     
     if (n == 0) return -1;
     if (n == 1) return 0;
     
     int c = 0;
     for(int i = 1; i < n; i ++){
         bool ok = knows(c, i);
         if (ok){
             c = i;
         }
     }
     
     for(int i = 0; i < n ;i ++){
         if (i != c){
             if (knows(c, i) || knows(i, c) == false ){
                 return -1;
             }
         }
     }
     
     return c;
 }
コード例 #13
0
void
RODFDetectorFlows::mesoJoin(const std::string& nid,
                            const std::vector<std::string>& oldids) {
    for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
        if (!knows(*i)) {
            continue;
        }
        std::vector<FlowDef>& flows = myFastAccessFlows[*i];
        size_t index = 0;
        for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
            addFlow(nid, t, flows[index++]); // !!!
        }
        myFastAccessFlows.erase(*i);
    }
}
コード例 #14
0
    int findCelebrity(int n) {

        if (n <= 0)
            return n;

        int candidate = 0;

        for(int i = 1; i<n; ++i) {
            if (knows(candidate, i))
                candidate = i;
        }

        for(int i = 0;i<n; ++i) {

            if (i == candidate)
                continue;

            if (!knows(i, candidate) || knows(candidate, i))
                return -1;
        }

        return candidate;

    }
コード例 #15
0
 int findCelebrity(int n) {
     // start coding at 2:35
     
     // double pointer
     int i = 0, j = n-1;
     while (i < j) {
         if (knows(i, j)) {
             i++;
         }
         else {
             j--;
         }
     }
     
     // verify it by a single loop
     for (int k = 0; k < n; k++) {
         if (k == i) continue;
         if (knows(i, k) || !knows(k, i)) return -1;
     }
     
     
     // return
     return i;
 }